时间:2021-05-23
Socketserver原理图
服务端:
import SocketServer #导入SocketServer,多线程并发由此类实现 class MySockServer(SocketServer.BaseRequestHandler): #定义一个类 def handle(self): #handle(self)方法是必须要定义的,可以看上面的说明 print 'Got a new connection from', self.client_address while True: data = self.request.recv(1024) #需要通过self的方法调用数据接收函数 if not data:break print 'recv:', data self.request.send(data.upper()) #需要通过self的方法调用数据接收函数 if __name__ == '__main__': #并非一定要用这样的方式,只是建议这样使用 HOST = '' #定义侦听本地地址口(多个IP地址情况下),这里表示侦听所有 PORT = 50007 #Server端开放的服务端口 s = SocketServer.ThreadingTCPServer((HOST, PORT), MySockServer) #调用SocketServer模块的多线程并发函数 s.serve_forever() #持续接受客户端的连接客户端:
import socket HOST = '192.168.1.13'PORT = 50007s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.connect((HOST, PORT)) while True: user_input = raw_input('msg to send:').strip() s.sendall(user_input) data = s.recv(1024) print 'Received', repr(data) s.close()服务端:
import socketserverimport structimport jsonimport osclass FtpServer(socketserver.BaseRequestHandler): coding='utf-8' server_dir='file_upload' max_packet_size=1024 BASE_DIR=os.path.dirname(os.path.abspath(__file__)) def handle(self): print(self.request) while True: data=self.request.recv(4) data_len=struct.unpack('i',data)[0] head_json=self.request.recv(data_len).decode(self.coding) head_dic=json.loads(head_json) # print(head_dic) cmd=head_dic['cmd'] if hasattr(self,cmd): func=getattr(self,cmd) func(head_dic) def put(self,args): file_path = os.path.normpath(os.path.join( self.BASE_DIR, self.server_dir, args['filename'] )) filesize = args['filesize'] recv_size = 0 print('----->', file_path) with open(file_path, 'wb') as f: while recv_size < filesize: recv_data = self.request.recv(self.max_packet_size) f.write(recv_data) recv_size += len(recv_data) print('recvsize:%s filesize:%s' % (recv_size, filesize))ftpserver=socketserver.ThreadingTCPServer(('127.0.0.1',8080),FtpServer)ftpserver.serve_forever()客户端:
import socketimport structimport jsonimport osclass MYTCPClient: address_family = socket.AF_INET socket_type = socket.SOCK_STREAM allow_reuse_address = False max_packet_size = 8192 coding='utf-8' request_queue_size = 5 def __init__(self, server_address, connect=True): self.server_address=server_address self.socket = socket.socket(self.address_family, self.socket_type) if connect: try: self.client_connect() except: self.client_close() raise def client_connect(self): self.socket.connect(self.server_address) def client_close(self): self.socket.close() def run(self): while True: inp=input(">>: ").strip() if not inp:continue l=inp.split() cmd=l[0] if hasattr(self,cmd): func=getattr(self,cmd) func(l) def put(self,args): cmd=args[0] filename=args[1] if not os.path.isfile(filename): print('file:%s is not exists' %filename) return else: filesize=os.path.getsize(filename) head_dic={'cmd':cmd,'filename':os.path.basename(filename),'filesize':filesize} print(head_dic) head_json=json.dumps(head_dic) head_json_bytes=bytes(head_json,encoding=self.coding) head_struct=struct.pack('i',len(head_json_bytes)) self.socket.send(head_struct) self.socket.send(head_json_bytes) send_size=0 with open(filename,'rb') as f: for line in f: self.socket.send(line) send_size+=len(line) print(send_size) else: print('upload successful')client=MYTCPClient(('127.0.0.1',8080))client.run()以上就是Python Socket多线程并发原理及实现的详细内容,更多关于Python Socket多线程并发的资料请关注其它相关文章!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
目的:是学习python多线程的工作原理,及通过抓取400张图片这种IO密集型应用来查看多线程效率对比importrequestsimporturlparsei
对于多线程的使用,我们经常是用thread来创建,比较繁琐.在Python中,可以使用map函数简化代码。map可以实现多任务的并发简单说明map()实现多线程
java高并发中volatile的实现原理摘要:在多线程并发编程中synchronized和Volatile都扮演着重要的角色,Volatile是轻量级的syn
前面已经演示了Python:使用threading模块实现多线程编程二两种方式起线程和Python:使用threading模块实现多线程编程三threading
Python里的多线程是假的多线程,不管有多少核,同一时间只能在一个核中进行操作!利用Python的多线程,只是利用CPU上下文切换的优势,看上去像是并发,其实