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