python 简单的多线程链接实现代码
(编辑:jimmy 日期: 2024/11/29 浏览:3 次 )
服务端:
#!/usr/bin/env import SocketServer class 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()
客户端:
#!/usr/bin/env python import socket 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 import datetime import SocketServer classmyMonitorHandler(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 客户端:
#!/usr/bin/envpython import socket 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-24 06:47:03.620356 :UP
From('192.168.1.104', 58033) : 2013-12-24 06:47:05.464851 :UP
From('192.168.1.104', 58034) : 2013-12-24 06:47:06.273092 :UP
二.服务器端添加时间标签后(判断30秒更新发送)
2.1 首先要有字典存放客户端的信息
先定义1个空字典
#!/usr/bin/env importdatetime importSocketServer host_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,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 import datetime import SocketServer host_status={} #定义个空子典用来存储客户端发送的信息和时间 f=open('client.txt') #文件的读取 while 1: line=f.readline().split() #文件的读取并分段 if len(line)==0:break #结尾退出 host_status[line[0]]= [] #初始化字典,把文件中的ip设置为字典的key f.close() classmyMonitorHandler(SocketServer.BaseRequestHandler): def handle(self): self.data=self.request.recv(1024).strip() ifself.client_address[0] in host_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 %s is't in the monitor list!" %self.client_address[0]#不在提示 for t,m inhost_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” 博客
下一篇:python实现实时监控文件的方法