llvm-project
51 строка · 1.2 Кб
1#!/usr/bin/env python
2from __future__ import absolute_import, division, print_function3import sys4from socket import *5from time import strftime6import datetime7
8
9def main():10if len(sys.argv) < 4:11print("completion_logger_server.py <listen address> <listen port> <log file>")12exit(1)13
14host = sys.argv[1]15port = int(sys.argv[2])16buf = 1024 * 817addr = (host, port)18
19# Create socket and bind to address20UDPSock = socket(AF_INET, SOCK_DGRAM)21UDPSock.bind(addr)22
23print("Listing on {0}:{1} and logging to '{2}'".format(host, port, sys.argv[3]))24
25# Open the logging file.26f = open(sys.argv[3], "a")27
28# Receive messages29while 1:30data, addr = UDPSock.recvfrom(buf)31if not data:32break33else:34f.write("{ ")35f.write(36'"time": "{0}"'.format(37datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")38)39)40f.write(', "sender": "{0}" '.format(addr[0]))41f.write(', "data": ')42f.write(data)43f.write(" }\n")44f.flush()45
46# Close socket47UDPSock.close()48
49
50if __name__ == "__main__":51main()52