blob: 3182c1c519eb28460614210d15800080857fbc2e [file] [log] [blame]
Ted Kremenek66cf36d2010-04-15 01:02:31 +00001#!/usr/bin/env python
2import sys
3from socket import *
4from time import localtime, strftime
5
6def main():
7 if len(sys.argv) < 4:
8 print "completion_logger_server.py <listen address> <listen port> <log file>"
9 exit(1)
10
11 host = sys.argv[1]
12 port = int(sys.argv[2])
13 buf = 1024 * 8
14 addr = (host,port)
15
16 # Create socket and bind to address
17 UDPSock = socket(AF_INET,SOCK_DGRAM)
18 UDPSock.bind(addr)
19
20 print "Listing on {0}:{1} and logging to '{2}'".format(host, port, sys.argv[3])
21
22 # Open the logging file.
23 f = open(sys.argv[3], "a")
24
25 # Receive messages
26 while 1:
27 data,addr = UDPSock.recvfrom(buf)
28 if not data:
29 break
30 else:
31 f.write(strftime("'%a, %d %b %Y %H:%M:%S' ", localtime()))
Ted Kremenek264b7f22010-04-15 06:32:15 +000032 f.write("'{0}' ".format(addr[0]))
Ted Kremenek66cf36d2010-04-15 01:02:31 +000033 f.write(data)
34 f.write('\n')
Ted Kremenek264b7f22010-04-15 06:32:15 +000035 f.flush()
Ted Kremenek66cf36d2010-04-15 01:02:31 +000036
37 # Close socket
38 UDPSock.close()
39
40if __name__ == '__main__':
41 main()