标签:and finish ddr 地址 continue input struct reading bind
基于tcp的套接字,关键就是两个循环,一个链接循环,一个通信循环
socketserver模块中分两大类:server类(解决链接问题)和request类(解决通信问题)
以下述代码为例,分析socketserver源码:
ftpserver=socketserver.ThreadingTCPServer((‘127.0.0.1‘,8080),FtpServer)
ftpserver.serve_forever()
基于tcp的socketserver我们自己定义的类中的
基于udp的socketserver我们自己定义的类中的
1 import socketserver #导入多并发模块 2 import struct 3 import json 4 import subprocess 5 import os 6 7 class MYTCPServer(socketserver.BaseRequestHandler): 8 9 max_packet_size = 8192 10 11 coding=‘utf-8‘ 12 BASE_DIR=os.path.dirname(os.path.abspath(__file__)) 13 14 server_dir=‘file_upload‘ 15 16 def handle(self): 17 while True: 18 try: 19 head_struct = self.request.recv(4) 20 if not head_struct:break 21 22 head_len = struct.unpack(‘i‘, head_struct)[0] 23 head_json = self.request.recv(head_len).decode(self.coding) 24 head_dic = json.loads(head_json) 25 26 print(head_dic) 27 #head_dic={‘cmd‘:‘put‘,‘filename‘:‘a.txt‘,‘filesize‘:123123} 28 cmd=head_dic[‘cmd‘] 29 if hasattr(self,cmd): 30 func=getattr(self,cmd) 31 func(head_dic) 32 except Exception: 33 break 34 35 def put(self,args): 36 file_path=os.path.normpath(os.path.join( 37 self.BASE_DIR, 38 self.server_dir, 39 args[‘filename‘] 40 )) 41 42 filesize=args[‘filesize‘] 43 recv_size=0 44 print(‘----->‘,file_path) 45 with open(file_path,‘wb‘) as f: 46 while recv_size < filesize: 47 recv_data=self.request.recv(self.max_packet_size) 48 f.write(recv_data) 49 recv_size+=len(recv_data) 50 print(‘recvsize:%s filesize:%s‘ %(recv_size,filesize)) 51 52 53 54 55 if __name__ == ‘__main__‘: 56 obj=socketserver.ThreadingTCPServer((‘127.0.0.1‘,8080),MYTCPServer) 57 obj.serve_forever()
1 import socket 2 import struct 3 import json 4 import os 5 6 7 8 class MYTCPClient: 9 address_family = socket.AF_INET 10 11 socket_type = socket.SOCK_STREAM 12 13 allow_reuse_address = False 14 15 max_packet_size = 8192 16 17 coding=‘utf-8‘ 18 19 request_queue_size = 5 20 21 def __init__(self, server_address, connect=True): 22 self.server_address=server_address 23 self.socket = socket.socket(self.address_family, 24 self.socket_type) 25 if connect: 26 try: 27 self.client_connect() 28 except: 29 self.client_close() 30 raise 31 32 def client_connect(self): 33 self.socket.connect(self.server_address) 34 35 def client_close(self): 36 self.socket.close() 37 38 def run(self): 39 while True: 40 inp=input(">>: ").strip() 41 if not inp:continue 42 l=inp.split() 43 cmd=l[0] 44 if hasattr(self,cmd): 45 func=getattr(self,cmd) 46 func(l) 47 48 49 def put(self,args): 50 cmd=args[0] 51 filename=args[1] 52 if not os.path.isfile(filename): 53 print(‘file:%s is not exists‘ %filename) 54 return 55 else: 56 filesize=os.path.getsize(filename) 57 58 head_dic={‘cmd‘:cmd,‘filename‘:os.path.basename(filename),‘filesize‘:filesize} 59 print(head_dic) 60 head_json=json.dumps(head_dic) 61 head_json_bytes=bytes(head_json,encoding=self.coding) 62 63 head_struct=struct.pack(‘i‘,len(head_json_bytes)) 64 self.socket.send(head_struct) 65 self.socket.send(head_json_bytes) 66 send_size=0 67 with open(filename,‘rb‘) as f: 68 for line in f: 69 self.socket.send(line) 70 send_size+=len(line) 71 print(send_size) 72 else: 73 print(‘upload successful‘) 74 75 76 77 78 client=MYTCPClient((‘127.0.0.1‘,8080)) 79 80 client.run()
python基础----socketserver多并发实现、FTP上传多并发
标签:and finish ddr 地址 continue input struct reading bind
原文地址:http://www.cnblogs.com/wangyongsong/p/6813570.html