diff --git a/utils/blpserver.py b/utils/blpserver.py index b732d53..51c970c 100755 --- a/utils/blpserver.py +++ b/utils/blpserver.py @@ -1,3 +1,4 @@ +#! /usr/bin/env python3 import socket import getopt @@ -5,84 +6,86 @@ import sys from blup.BLP import BLPServer import blup.BLP -import blup.output - +import blup.output def printUsage(errMsg=None): - if errMsg is not None: - print 'error: %s\n' % (errMsg) - print 'usage: %s [OPTIONS]' % (sys.argv[0]) - print 'supported options:' - print ' -a ADDR address to listen for packets (default: 127.0.0.1)' - print ' --address ADDR\n' - print ' -p PORT port to listen for packets (default: 4242)' - print ' --port PORT\n' - print ' -o OUTPUT where to put the recieved frames (default: shell)' - print ' --output OUTPUT\n' - print ' --eblp use EBLP instead of BLP\n' - print ' --e3blp use E3BLP instead of BLP\n' - print ' -h print this text' - print ' --help' - + if errMsg is not None: + print('error: %s\n' % (errMsg)) + print('usage: %s [OPTIONS]' % (sys.argv[0])) + print('supported options:') + print(' -a ADDR address to listen for packets', + '(default: 127.0.0.1)') + print(' --address ADDR\n') + print(' -p PORT port to listen for packets (default: 4242)') + print(' --port PORT\n') + print(' -o OUTPUT where to put the recieved frames', + '(default: shell)') + print(' --output OUTPUT\n') + print(' --eblp use EBLP instead of BLP\n') + print(' --e3blp use E3BLP instead of BLP\n') + print(' -h print this text') + print(' --help') def main(): - try: - (opts, args) = getopt.gnu_getopt(sys.argv, 'ha:p:o:', ['help', 'address=', 'port=', 'output=', 'eblp', 'e3blp']) - opts = dict(opts) - except getopt.GetoptError as e: - printUsage(e.msg) - sys.exit(1) - - if opts.has_key('--help'): - printUsage() - sys.exit(0) - - if opts.has_key('-a'): - addr = opts['-a'] - elif opts.has_key('--address'): - addr = opts['--address'] - else: - addr = '127.0.0.1' - - if opts.has_key('-p'): - port = opts['-p'] - elif opts.has_key('--port'): - port = opts['--port'] - else: - port = 4242 - - if opts.has_key('-o'): - output = opts['-o'] - elif opts.has_key('--output'): - output = opts['--output'] - else: - output = 'shell' - - if opts.has_key('--eblp'): - protocol = blup.BLP.PROTO_EBLP - elif opts.has_key('--e3blp'): - protocol = blup.BLP.PROTO_E3BLP - else: - protocol = blup.BLP.PROTO_BLP - - if opts.has_key('--eblp') and opts.has_key('--e3blp'): - printUsage('please only specify one of --eblp or --e3blp') - sys.exit(1) - - try: - port = int(port) - except ValueError: - printUsage('invalid port specified') - sys.exit(1) - - out = blup.output.getOutput(output) - srv = BLPServer(output=out, addr=addr, port=port, protocol=protocol) - - try: - srv.serve() - except KeyboardInterrupt: - pass + try: + (opts, args) = getopt.gnu_getopt(sys.argv, 'ha:p:o:', + ['help', 'address=', 'port=', + 'output=', 'eblp', 'e3blp']) + opts = dict(opts) + except getopt.GetoptError as e: + printUsage(e.msg) + sys.exit(1) + + if '--help' in opts: + printUsage() + sys.exit(0) + + if '-a' in opts: + addr = opts['-a'] + elif '--address' in opts: + addr = opts['--address'] + else: + addr = '127.0.0.1' + + if '-p' in opts: + port = opts['-p'] + elif '--port' in opts: + port = opts['--port'] + else: + port = 4242 + + if '-o' in opts: + output = opts['-o'] + elif '--output' in opts: + output = opts['--output'] + else: + output = 'shell' + + if '--eblp' in opts: + protocol = blup.BLP.PROTO_EBLP + elif '--e3blp' in opts: + protocol = blup.BLP.PROTO_E3BLP + else: + protocol = blup.BLP.PROTO_BLP + + if '--eblp' in opts and '--e3blp' in opts: + printUsage('please only specify one of --eblp or --e3blp') + sys.exit(1) + + try: + port = int(port) + except ValueError: + printUsage('invalid port specified') + sys.exit(1) + + out = blup.output.getOutput(output) + srv = BLPServer(output=out, addr=addr, port=port, protocol=protocol) + + try: + srv.serve() + except KeyboardInterrupt: + pass if __name__ == '__main__': - main() + main()