实现socket并发
import socketserver buffer_size = 1024 ip_port = ('127.0.0.1',8080) class MyServer(socketserver.BaseRequestHandler): def handle(self): print (self.request) print (self.client_address) while True: try: msg = self.request.recv(buffer_size) if not msg: break print ('收到服务端的消息是:',msg) self.request.sendall(msg.upper()) except Exception as e: print (e) if __name__ == '__main__': tcp_server = socketserver.ThreadingTCPServer(ip_port,MyServer) tcp_server.serve_forever()
import socketserver buffer_size = 1024 ip_port = ('127.0.0.1',8080) class MyServer(socketserver.BaseRequestHandler): def handle(self): print (self.request) print (self.client_address) while True: try: msg = self.request.recv(buffer_size) if not msg: break print ('收到服务端的消息是:',msg) self.request.sendall(msg.upper()) except Exception as e: print (e) if __name__ == '__main__': tcp_server = socketserver.ThreadingTCPServer(ip_port,MyServer) tcp_server.serve_forever()
FTP server和client开发示例: