Ted Kremenek | 66cf36d | 2010-04-15 01:02:31 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | import sys |
| 3 | from socket import * |
| 4 | from time import localtime, strftime |
| 5 | |
| 6 | def 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())) |
| 32 | f.write(data) |
| 33 | f.write('\n') |
| 34 | |
| 35 | # Close socket |
| 36 | UDPSock.close() |
| 37 | |
| 38 | if __name__ == '__main__': |
| 39 | main() |