python 简单的多线程链接实现代码
服务端:
#!/usr/bin/env importSocketServer classmyMonitorHandler(SocketServer.BaseRequestHandler): defhandle(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()
客户端:
#!/usr/bin/envpython importsocket host,port="10.23.30.191",18000 s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect((host,port)) s.send("UP") ~
服务端运行结果:
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简单的多线程链接(二)
一。简单的多线程(服务器端添加了时间标签)
1.1服务端
#!/usr/bin/env importdatetime importSocketServer classmyMonitorHandler(SocketServer.BaseRequestHandler): defhandle(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客户端:
#!/usr/bin/envpython importsocket host,port="192.168.1.103",18000 s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect((host,port)) s.send("UP")
结果:
From('192.168.1.104',58032):2013-12-2406:47:03.620356 :UP
From('192.168.1.104',58033):2013-12-2406:47:05.464851 :UP
From('192.168.1.104',58034):2013-12-2406:47:06.273092 :UP
二.服务器端添加时间标签后(判断30秒更新发送)
2.1首先要有字典存放客户端的信息
先定义1个空字典
#!/usr/bin/env importdatetime importSocketServer host_status={} f=open('client.txt') while1: line=f.readline().split() iflen(line)==0:break host_status[line[0]]=[] f.close() classmyMonitorHandler(SocketServer.BaseRequestHandler): defhandle(self): self.data=self.request.recv(1024).strip() ifself.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'tinthemonitorlist!"%self.client_address[0] fort,minhost_status.items(): printt,m if__name__=="__main__": HOST,PORT='0.0.0.0',18000 server=SocketServer.ThreadingTCPServer((HOST,PORT),myMonitorHandler) server.serve_forever() server.close()
解释:
#!/usr/bin/env importdatetime importSocketServer host_status={}#定义个空子典用来存储客户端发送的信息和时间 f=open('client.txt')#文件的读取 while1: line=f.readline().split()#文件的读取并分段 iflen(line)==0:break#结尾退出 host_status[line[0]]=[]#初始化字典,把文件中的ip设置为字典的key f.close() classmyMonitorHandler(SocketServer.BaseRequestHandler): defhandle(self): self.data=self.request.recv(1024).strip() ifself.client_address[0]inhost_status.keys():#如果获取客户端ip在字典的列表的key中 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)#输出客户端的ip,连接时间,发送的内容 else: print"sorry,IP%sis'tinthemonitorlist!"%self.client_address[0]#不在提示 fort,minhost_status.items():#字典输出 printt,m#字典的输出 if__name__=="__main__": HOST,PORT='0.0.0.0',18000 server=SocketServer.ThreadingTCPServer((HOST,PORT),myMonitorHandler) server.serve_forever() server.close()
本文出自“lzt417”博客