Python simple multi-thread link implementation code, python multi-thread
Server:
#!/usr/bin/envimport SocketServerclass myMonitorHandler(SocketServer.BaseRequestHandler): def handle(self): self.data=self.request.recv(1024).strip() print "From %s : %s" %(self.client_address,self.data)if __name__=="__main__": HOST,PORT='0.0.0.0',18000 server=SocketServer.ThreadingTCPServer((HOST,PORT),myMonitorHandler) server.serve_forever() server.close()
Client:
#!/usr/bin/env pythonimport sockethost,port="10.23.30.191",18000s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)s.connect((host,port))s.send("UP")~
Server running result:
From ('10. 23.30.61 ', 45809): UP
From ('10. 23.30.61 ', 45810): UP
From ('10. 23.30.61 ', 45811): UP
From ('10. 23.30.61 ', 45812): UP
From ('10. 23.30.61 ', 45813): UP
From ('10. 23.30.61 ', 45814): UP
From ('10. 23.30.61 ', 45815): UP
Python simple multi-thread Link (2)
I. Simple multi-thread (Time tag added on the server side)
1.1 Server
#!/usr/bin/envimport datetimeimport SocketServerclassmyMonitorHandler(SocketServer.BaseRequestHandler): def handle(self): self.data=self.request.recv(1024).strip() print "From %s : %s :%s"%(self.client_address,datetime.datetime.now(),self.data)if__name__=="__main__": HOST,PORT='0.0.0.0',18000 server=SocketServer.ThreadingTCPServer((HOST,PORT),myMonitorHandler) server.serve_forever() server.close()
1.2 client:
#!/usr/bin/envpythonimport sockethost,port="192.168.1.103",18000s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)s.connect((host,port))s.send("UP")
Result:
From ('1970. 168.1.104 ', 192): 58032 06:47:03. 620356: UP
From ('1970. 168.1.104 ', 192): 58033 06:47:05. 464851: UP
From ('1970. 168.1.104 ', 192): 58034 06:47:06. 273092: UP
2. After the time tag is added to the server side (30 s update and sending are judged)
2.1 first, you must have a dictionary to store client information.
Define an empty dictionary first
#!/usr/bin/envimportdatetimeimportSocketServerhost_status={}f=open('client.txt')while 1: line=f.readline().split() if len(line)==0:break host_status[line[0]]= []f.close()classmyMonitorHandler(SocketServer.BaseRequestHandler): def handle(self): self.data=self.request.recv(1024).strip() if self.client_address[0] inhost_status.keys(): host_status[self.client_address[0]].append((datetime.datetime.now(),self.data)) print "From %s :%s :%s"%(self.client_address,datetime.datetime.now(),self.data) else: print "sorry,IP %sis't in the monitor list!" %self.client_address[0] for t,m in host_status.items(): print t,mif__name__=="__main__": HOST,PORT='0.0.0.0',18000 server=SocketServer.ThreadingTCPServer((HOST,PORT),myMonitorHandler) server.serve_forever() server.close()
Explanation:
#! /Usr/bin/envimport datetimeimport SocketServerhost_status ={} define an empty sub-code to store the information sent by the client and the time f=open('client.txt ') # Read the file while 1: line = f. readline (). split () # File Reading and segmentation if len (line) = 0: break # exit host_status [line [0] = [] # initialize the dictionary, set the IP address in the file to the dictionary keyf. close () classmyMonitorHandler (SocketServer. baseRequestHandler): def handle (self): self. data = self. request. recv (1, 1024 ). strip () ifself. client_address [0] in host_status.keys () : # If you obtain the Client ip address in the key of the dictionary list, host_status [self. client_address [0]. append (datetime. datetime. now (), self. data) # print "From % s: % s" % (self. client_address, datetime. datetime. now (), self. data) # output Client ip address, connection time, sent content else: print "sorry, IP % s is not in the monitor list! "% Self. client_address [0] # Do not prompt for t, m inhost_status.items (): # dictionary output printt, m # dictionary output if _ name __= = "_ main __": HOST, PORT = '0. 0.0.0 ', 18000 server = SocketServer. threadingTCPServer (HOST, PORT), myMonitorHandler) server. serve_forever () server. close ()
This article is from the "lzt417" blog