# Client-side Import Socketip_port = (' 127.0.0.1 ', 8091) SK = Socket.socket () sk.connect (ip_port) print ("Clients start:") While TRUE:INP = input (">>>") Sk.sendall (bytes (INP, "UTF8")) if InP = = ' exit ': Break Server_r Esponse = SK.RECV (1024x768) print (str (server_response, "UTF8")) Sk.close () # Server-side import Socketserverclass MyServer (Socke Tserver. Baserequesthandler): def handle (self): print ("Server startup ...") while true:conn = Self.request Print (self.client_address) while true:client_data = Conn.recv (1024x768) print ( STR (client_data, "UTF8")) print ("Waiting ...") Server_response = input (">>>") Conn.sendall (Bytes (server_response, "UTF8")) conn.close () if __name__ = = ' __main__ ': Server = socket Server. Threadingtcpserver ((' 127.0.0.1 ', 8091), MyServer) Server.serve_forever () # This will execute the handle method, so the handle method is to write the program logic.
Create a socketserver at least one of the following steps
First, you must create a request handler class by subclassing the Baserequesthandlerclass and overriding its handle () meth Od This method would process incoming requests.
Second, you must instantiate one of the server classes, passing it the server ' s address and the request handler class.
Then call the Handle_request () or Serve_forever () method of the server object to process one or many requests.
Finally, call Server_close () to close the socket.
Python simple multi-threaded Chat