import gzip import select import socket def receiveClientLogs(port): try: # open a server socket which is used to receive logs s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind(('', port)) s.listen(1) # define a list for client sockets, client ip addresses and client log files sockList = [] addrList = [] fileList = [] # keep running this thread while 1: # accept connections from requesting clients readList = []; writeList = []; errorList = [] readList, writeList, errorList = select.select([s], [], [], 0.001) # for each connecting client, add the socket to socket list, add the ip # to the address list, and open a logfile which is added to the file list for client in readList: sock, addr = s.accept() sock.setblocking(0) sockList.extend([sock]) addrList.extend([addr]) fl = open(addr[0] + ".log.gz", 'wb'); fileList.extend([fl]) # process all clients that have sent log information readList, writeList, errorList = select.select(sockList, [], [], 0.001) for client in readList: buffer = client.recv(1024) # if the buffer is filled with some information, add it to the logfile # if the buffer is empty, then the client disconnected, so we clean up if buffer: fl.write(buffer) else: index = sockList.index(client) fileList[index].close() sockList[index].close() sockList.remove(sockList[index]) addrList.remove(addrList[index]) fileList.remove(fileList[index]) except AttributeError: # thread shutdown return; def sendLog(logfile, host, port): s = None for res in socket.getaddrinfo(host, port,socket.AF_UNSPEC,socket.SOCK_STREAM): af, socktype, proto, canonname, sa = res try: s = socket.socket(af, socktype, proto) except socket.error, msg: s = None continue try: s.connect(sa) except socket.error, msg: s.close() s = None continue break if s is None: print 'Failed to send log (could not open socket to server)' return try: gzipFile(logfile) fl = open(logfile + ".gz", 'rb') buf = fl.read() s.send(buf) s.close() except: print "File " + logfile + " does not exist." # opens the requested file, gzips it and saves it back to disk. The zipped # file is saved as the original filename plus the '.gz' extension. # filename -> filename.gz def gzipFile(filename): # read the input file fileContent = "" fl = open(filename, 'rb') fileContent = fl.read() # gzip the file gzfl = gzip.GzipFile(filename + ".gz", 'wb') gzfl.write(fileContent) gzfl.close() # opens the requested file, gunzips it and saves it back to disk. The unzipped # file is saved as the original filename minus the '.gz' extension. # filename.gz -> filename def gunzipFile(filename): # decompress the file gzfl = gzip.GzipFile(filename, 'rb') fileContent = gzfl.read() gzfl.close() # write to file fl = open(filename[:-3], 'w') fl.write(fileContent) fl.close()