并发实例

 

并发服务端:

 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()

并发客户端

 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()

 

posted @ 2018-09-06 16:26  ★行者尚★  阅读(381)  评论(0编辑  收藏  举报