server.py 973 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/python
  2. from __future__ import print_function
  3. import datetime
  4. import socket
  5. import random, string
  6. def myrandom(length):
  7. return ''.join(random.choice(string.lowercase) for i in range(length))
  8. if __name__ == '__main__':
  9. TCP_IP = '::'
  10. TCP_PORT = 1337
  11. BUFFER_SIZE = 1024
  12. s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
  13. s.bind((TCP_IP, TCP_PORT))
  14. s.listen(1)
  15. while 1:
  16. conn, addr = s.accept()
  17. report_id = myrandom(10)
  18. filename = 'reports/' + datetime.date.today().strftime('%Y-%m-%d_') + report_id + '.gz'
  19. f = open(filename, 'w')
  20. while 1:
  21. data = conn.recv(BUFFER_SIZE)
  22. if not data: break
  23. if data is "ende":
  24. break
  25. f.write(data) # python will convert \n to os.linesep
  26. f.flush()
  27. f.close()
  28. # send reply to reportee
  29. conn.send(report_id)
  30. conn.close()
  31. print 'new report:', filename
  32. pass