併發實例

 

併發服務端:併發

 1 import pickle  2 import hashlib  3 import socketserver  4 class Myserver(socketserver.BaseRequestHandler):  5     def handle(self):  6 
 7         while 1:  8             content_long=self.request.recv(4)  9             pickle_content=self.request.recv(1024) 10             pickle_loads_content=pickle.loads(pickle_content) 11             # {'work': 'as', 'file': 'a.txt', 'long': 17}
12             # print(pickle_loads_content)
13             self.request.send(b'ok') 14             sended=0 15             s=b''
16             total_long=pickle_loads_content['long'] 17             md5=hashlib.md5() 18             i=0 19             while sended<int(pickle_loads_content['long']): 20                 i+=1
21                 # print(i)
22                 data=self.request.recv(1024) 23  md5.update(data) 24                 s+=data 25                 sended+=len(data)+1
26                 # print(sended)
27             self.request.send(md5.hexdigest().encode('utf-8')) 28             break
29 server=socketserver.ThreadingTCPServer(('192.168.13.137', 8027),Myserver) 30 # 建立套接字對象
31 # bind
32 # listen
33 server.serve_forever()

併發客戶端socket

 1 import pickle  2 import hashlib  3 import socketserver  4 class Myserver(socketserver.BaseRequestHandler):  5     def handle(self):  6 
 7         while 1:  8             content_long=self.request.recv(4)  9             pickle_content=self.request.recv(1024) 10             pickle_loads_content=pickle.loads(pickle_content) 11             # {'work': 'as', 'file': 'a.txt', 'long': 17}
12             # print(pickle_loads_content)
13             self.request.send(b'ok') 14             sended=0 15             s=b''
16             total_long=pickle_loads_content['long'] 17             md5=hashlib.md5() 18             i=0 19             while sended<int(pickle_loads_content['long']): 20                 i+=1
21                 # print(i)
22                 data=self.request.recv(1024) 23  md5.update(data) 24                 s+=data 25                 sended+=len(data)+1
26                 # print(sended)
27             self.request.send(md5.hexdigest().encode('utf-8')) 28             break
29 server=socketserver.ThreadingTCPServer(('192.168.13.137', 8027),Myserver) 30 # 建立套接字對象
31 # bind
32 # listen
33 server.serve_forever()
相關文章
相關標籤/搜索