import socket import getopt import sys from blup.BLP import BLPServer import blup.BLP 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' 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 if __name__ == '__main__': main()