1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- #!/usr/bin/python
- from __future__ import print_function
- import datetime
- import socket
- import random, string
- import daemon, getopt, sys
- import daemon.pidlockfile
- def myrandom(length):
- return ''.join(random.choice(string.lowercase) for i in range(length))
- def server(port, bindTo):
- BUFFER_SIZE = 1024
-
- s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
- s.bind((bindTo, port))
- s.listen(1)
- print('DebugReport server listening on [{0}]:{1}'.format(bindTo, port))
- while 1:
- conn, addr = s.accept()
-
- report_id = myrandom(10)
- filename = 'reports/' + datetime.date.today().strftime('%Y-%m-%d_') + report_id + '.gz'
- f = open(filename, 'w')
- while 1:
- data = conn.recv(BUFFER_SIZE)
- if not data: break
- f.write(data) # python will convert \n to os.linesep
- f.flush()
- f.close()
- # send reply to reportee
- conn.send(report_id)
- conn.close()
- print('new report "{0}" from [{2}]:{3} stored as "{1}"'.format(report_id, filename, addr[0], addr[1]))
- pass
- if __name__ == '__main__':
- try:
- opts, args = getopt.getopt(sys.argv[1:], "dp:b:", ["do-not-daemonize", "port", "bind-to"])
- except:
- print ('Unrecognized option')
- sys.exit(2)
- daemonize = True
- port = 1337
- bindTo = '::'
- for opt, arg in opts:
- if opt in ("-d", "--do-not-daemonize"):
- daemonize = False
- elif opt in ("-p", "--port"):
- port = int(arg)
- elif opt in ("-b", "--bind-to"):
- try:
- socket.inet_aton(arg)
- bindTo = str(arg)
- except:
- print('Invalid IPAdress. Using default ::')
- else:
- assert False
-
- if daemonize == False:
- server(port, bindTo)
- else:
- daemonContext = daemon.DaemonContext(pidfile = daemon.pidlockfile.PIDLockFile("/var/run/ffpb-debugserver.pid"))
- with daemonContext:
- server(port, bindTo)
|