Browse Source

Python3ified and PEP8ed blpserver

master
klonfish 6 years ago
parent
commit
1cc28fadcc
  1. 149
      utils/blpserver.py

149
utils/blpserver.py

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

Loading…
Cancel
Save