Update: use argparse instead of getopt
This commit is contained in:
parent
8a3adfc344
commit
0735859d1f
2 changed files with 42 additions and 19 deletions
33
local.py
33
local.py
|
@ -31,7 +31,7 @@ import sys
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import getopt
|
import argparse
|
||||||
|
|
||||||
def get_table(key):
|
def get_table(key):
|
||||||
m = hashlib.md5()
|
m = hashlib.md5()
|
||||||
|
@ -130,16 +130,27 @@ if __name__ == '__main__':
|
||||||
PORT = config['local_port']
|
PORT = config['local_port']
|
||||||
KEY = config['password']
|
KEY = config['password']
|
||||||
|
|
||||||
optlist, args = getopt.getopt(sys.argv[1:], 's:p:k:l:')
|
parser = argparse.ArgumentParser(
|
||||||
for key, value in optlist:
|
description='ShadowSocks Client'
|
||||||
if key == '-p':
|
)
|
||||||
REMOTE_PORT = int(value)
|
parser.add_argument('-p', '--port', action='store', help='remote port', type=int)
|
||||||
elif key == '-k':
|
parser.add_argument('-k', '--key', action='store', help='password', type=unicode)
|
||||||
KEY = value
|
parser.add_argument('-l', '--localport', action='store', help='local port', type=int)
|
||||||
elif key == '-l':
|
parser.add_argument('-s', '--server', action='store', help='server ip address', type=unicode)
|
||||||
PORT = int(value)
|
if not len(sys.argv):
|
||||||
elif key == '-s':
|
parser.print_help()
|
||||||
SERVER = value
|
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',
|
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s',
|
||||||
datefmt='%Y-%m-%d %H:%M:%S', filemode='a+')
|
datefmt='%Y-%m-%d %H:%M:%S', filemode='a+')
|
||||||
|
|
28
server.py
28
server.py
|
@ -31,7 +31,7 @@ import sys
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import getopt
|
import argparse
|
||||||
|
|
||||||
def get_table(key):
|
def get_table(key):
|
||||||
m = hashlib.md5()
|
m = hashlib.md5()
|
||||||
|
@ -106,12 +106,25 @@ if __name__ == '__main__':
|
||||||
PORT = config['server_port']
|
PORT = config['server_port']
|
||||||
KEY = config['password']
|
KEY = config['password']
|
||||||
|
|
||||||
optlist, args = getopt.getopt(sys.argv[1:], 'p:k:')
|
parser = argparse.ArgumentParser(
|
||||||
for key, value in optlist:
|
description='ShadowSocks Server'
|
||||||
if key == '-p':
|
)
|
||||||
PORT = int(value)
|
parser.add_argument('-p', '--port', action='store', help='port', type=int)
|
||||||
elif key == '-k':
|
parser.add_argument('-k', '--key', action='store', help='password', type=unicode)
|
||||||
KEY = value
|
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',
|
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s',
|
||||||
datefmt='%Y-%m-%d %H:%M:%S', filemode='a+')
|
datefmt='%Y-%m-%d %H:%M:%S', filemode='a+')
|
||||||
|
@ -127,4 +140,3 @@ if __name__ == '__main__':
|
||||||
server.serve_forever()
|
server.serve_forever()
|
||||||
except socket.error, e:
|
except socket.error, e:
|
||||||
logging.error(e)
|
logging.error(e)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue