| 1 | #!/usr/bin/env python |
| 2 | from __future__ import absolute_import, division, print_function |
| 3 | import sys |
| 4 | from socket import * |
| 5 | from time import strftime |
| 6 | import datetime |
| 7 | |
| 8 | def main(): |
| 9 | if len(sys.argv) < 4: |
| 10 | print("completion_logger_server.py <listen address> <listen port> <log file>") |
| 11 | exit(1) |
| 12 | |
| 13 | host = sys.argv[1] |
| 14 | port = int(sys.argv[2]) |
| 15 | buf = 1024 * 8 |
| 16 | addr = (host,port) |
| 17 | |
| 18 | # Create socket and bind to address |
| 19 | UDPSock = socket(AF_INET,SOCK_DGRAM) |
| 20 | UDPSock.bind(addr) |
| 21 | |
| 22 | print("Listing on {0}:{1} and logging to '{2}'".format(host, port, sys.argv[3])) |
| 23 | |
| 24 | # Open the logging file. |
| 25 | f = open(sys.argv[3], "a") |
| 26 | |
| 27 | # Receive messages |
| 28 | while 1: |
| 29 | data,addr = UDPSock.recvfrom(buf) |
| 30 | if not data: |
| 31 | break |
| 32 | else: |
| 33 | f.write("{ "); |
| 34 | f.write("\"time\": \"{0}\"".format(datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'))) |
| 35 | f.write(", \"sender\": \"{0}\" ".format(addr[0])) |
| 36 | f.write(", \"data\": ") |
| 37 | f.write(data) |
| 38 | f.write(" }\n") |
| 39 | f.flush() |
| 40 | |
| 41 | # Close socket |
| 42 | UDPSock.close() |
| 43 | |
| 44 | if __name__ == '__main__': |
| 45 | main() |
| 46 | |