socket_sever实现多客户端并发
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import socketserver 4 class mysever(socketserver.BaseRequestHandler): 5 def handle(self): 6 conn = self.request 7 # 和之前创建socket是一样的 8 conn.sendall(bytes('欢迎致电10086', encoding='utf-8')) 9 while True: 10 ret_bytes = conn.recv(1024) 11 ret_str = str(ret_bytes, encoding='utf-8') 12 if ret_str == 'q': 13 break 14 conn.sendall(bytes(ret_str + 'ok', encoding='utf-8')) 15 16 if __name__ =='__main__': 17 sever = socketserver.ThreadingTCPServer(('127.0.0.1',9999,),mysever) 18 sever.serve_forever()