python 簡單的多線程連結實現代碼,python多線程

來源:互聯網
上載者:User

python 簡單的多線程連結實現代碼,python多線程

服務端:

#!/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()

用戶端:

#!/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")~ 

服務端運行結果:
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/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 用戶端:

#!/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")

結果:
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/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()

解釋:

#!/usr/bin/envimport datetimeimport SocketServerhost_status={} #定義個空子典用來儲存用戶端發送的資訊和時間f=open('client.txt') #檔案的讀取while 1:    line=f.readline().split() #檔案的讀取並分段    if len(line)==0:break  #結尾退出host_status[line[0]]= [] #初始化字典,把檔案中的ip設定為字典的keyf.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” 部落格

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.