SocketServer uses IO multiplexing and "multi-threading" and "multi-process" internally, so as to realize the Socket server that processes multiple client requests concurrently. That is: When each client requests to connect to the server, the socket server will create a "thread" or "process" on the server, which is specially responsible for processing all requests of the current client.
1.ThreadingTCPServer -----------(for threading)
The socket server implemented by ThreadingTCPServer will create a "thread" for each client, which is used to interact with the client
import socketserver class MyTCPHander (socketserver.BaseRequestHandler): def handle(self): while True: try: self.data = self.request.recv(1024).strip() print("{} wrote:".format(self.client_address[0])) print(self.data) self.request.sendall(self.data.upper()) except ConnectionResetError as e: print("err", e) break if __name__ == "__main__": HOST , PORT = "localhost" , 9999 #MyTCPHander is the tcp processing function we wrote ourselves, which is a thread server = socketserver.ThreadingTCPServer((HOST , PORT) , MyTCPHander) server.serve_forever()Among them, in addition to inheriting from socketserver.BaseRequestHandler, the created class must define a method named handler