From 0735859d1f1002dfb7562de1a36473cc8365a0b2 Mon Sep 17 00:00:00 2001 From: yegle Date: Mon, 10 Dec 2012 00:10:32 -0500 Subject: [PATCH 1/3] Update: use argparse instead of getopt --- local.py | 33 ++++++++++++++++++++++----------- server.py | 28 ++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/local.py b/local.py index bd6e09d..45937f0 100755 --- a/local.py +++ b/local.py @@ -31,7 +31,7 @@ import sys import os import json import logging -import getopt +import argparse def get_table(key): m = hashlib.md5() @@ -130,16 +130,27 @@ if __name__ == '__main__': PORT = config['local_port'] KEY = config['password'] - optlist, args = getopt.getopt(sys.argv[1:], 's:p:k:l:') - for key, value in optlist: - if key == '-p': - REMOTE_PORT = int(value) - elif key == '-k': - KEY = value - elif key == '-l': - PORT = int(value) - elif key == '-s': - SERVER = value + parser = argparse.ArgumentParser( + description='ShadowSocks Client' + ) + parser.add_argument('-p', '--port', action='store', help='remote port', type=int) + parser.add_argument('-k', '--key', action='store', help='password', type=unicode) + parser.add_argument('-l', '--localport', action='store', help='local port', type=int) + parser.add_argument('-s', '--server', action='store', help='server ip address', type=unicode) + if not len(sys.argv): + parser.print_help() + sys.exit(1) + + args = parser.parse_args() + + if args.server: + SERVER = args.server + if args.port: + REMOTE_PORT = args.port + if args.key: + KEY = args.key + if args.localport: + PORT = args.localport logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filemode='a+') diff --git a/server.py b/server.py index e9e8944..acbc0fa 100755 --- a/server.py +++ b/server.py @@ -31,7 +31,7 @@ import sys import os import json import logging -import getopt +import argparse def get_table(key): m = hashlib.md5() @@ -106,12 +106,25 @@ if __name__ == '__main__': PORT = config['server_port'] KEY = config['password'] - optlist, args = getopt.getopt(sys.argv[1:], 'p:k:') - for key, value in optlist: - if key == '-p': - PORT = int(value) - elif key == '-k': - KEY = value + parser = argparse.ArgumentParser( + description='ShadowSocks Server' + ) + parser.add_argument('-p', '--port', action='store', help='port', type=int) + parser.add_argument('-k', '--key', action='store', help='password', type=unicode) + if not len(sys.argv): + parser.print_help() + sys.exit(1) + + args = parser.parse_args() + + if args.key: + KEY = args.key + + if args.port: + PORT = args.port + + print(KEY, PORT) + sys.exit(0) logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filemode='a+') @@ -127,4 +140,3 @@ if __name__ == '__main__': server.serve_forever() except socket.error, e: logging.error(e) - From a61c718a33d821af90aac44dc3385fff956c534a Mon Sep 17 00:00:00 2001 From: yegle Date: Mon, 10 Dec 2012 00:15:24 -0500 Subject: [PATCH 2/3] Delete unused test code --- server.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/server.py b/server.py index acbc0fa..faaeae2 100755 --- a/server.py +++ b/server.py @@ -123,9 +123,6 @@ if __name__ == '__main__': if args.port: PORT = args.port - print(KEY, PORT) - sys.exit(0) - logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filemode='a+') From dda832659250ddd440e7830e187c4f8ccdeb4678 Mon Sep 17 00:00:00 2001 From: yegle Date: Mon, 10 Dec 2012 00:52:51 -0500 Subject: [PATCH 3/3] Refactoring the names used in config file --- config.json | 6 +++--- local.py | 19 +++++++------------ server.py | 14 ++++++-------- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/config.json b/config.json index 62a3a65..47e7219 100644 --- a/config.json +++ b/config.json @@ -1,7 +1,7 @@ { "server":"localhost", - "server_port":8388, - "local_port":1080, + "port":8388, + "localport":1080, "password":"barfoo!", "timeout":60 -} \ No newline at end of file +} diff --git a/local.py b/local.py index 45937f0..1f71b28 100755 --- a/local.py +++ b/local.py @@ -125,10 +125,6 @@ if __name__ == '__main__': with open('config.json', 'rb') as f: config = json.load(f) - SERVER = config['server'] - REMOTE_PORT = config['server_port'] - PORT = config['local_port'] - KEY = config['password'] parser = argparse.ArgumentParser( description='ShadowSocks Client' @@ -142,15 +138,14 @@ if __name__ == '__main__': sys.exit(1) args = parser.parse_args() + d = vars(args) + _config = dict((k,d[k]) for k in d if d[k]) + config.update(_config) - if args.server: - SERVER = args.server - if args.port: - REMOTE_PORT = args.port - if args.key: - KEY = args.key - if args.localport: - PORT = args.localport + SERVER = config['server'] + REMOTE_PORT = config['port'] + PORT = config['localport'] + KEY = config['password'] logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filemode='a+') diff --git a/server.py b/server.py index faaeae2..6a8e45c 100755 --- a/server.py +++ b/server.py @@ -102,10 +102,6 @@ if __name__ == '__main__': with open('config.json', 'rb') as f: config = json.load(f) - SERVER = config['server'] - PORT = config['server_port'] - KEY = config['password'] - parser = argparse.ArgumentParser( description='ShadowSocks Server' ) @@ -116,12 +112,14 @@ if __name__ == '__main__': sys.exit(1) args = parser.parse_args() + d = vars(args) + _config = dict((k,d[k]) for k in d if d[k]) - if args.key: - KEY = args.key + config.update(_config) - if args.port: - PORT = args.port + SERVER = config['server'] + PORT = config['port'] + KEY = config['password'] logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filemode='a+')