Why use Socketserver
Although Python's built-in sockets and threading modules enable simple multithreaded servers, they can be used casually in informal environments, but it's not enough if you want to use them in a production environment.
Python is thoughtful and provides a "socketserver" module to meet our needs for multi-threaded Web servers. The socketserver uses IO multiplexing and multithreading/process mechanisms internally, enabling the socket service side to handle multiple client requests concurrently. When each client request connects to the server, a "thread" or "process" is created on the Socketserver service side to handle all requests from the current client.
Use Socketserver essentials
- Create an inherited from "Socketserver. Baserequesthandler "of the class;
- This class must rewrite a method named "Handle", not another name!
- This new class, along with the IP and port of the server, is passed as a parameter to the "Threadingtcpserver ()" construct;
- Manually start "Threadingtcpserver".
Chestnut Service End
ImportSocketserver#must inherit socketserver. Baserequesthandler classclassMysockserver (socketserver. Baserequesthandler):defhandle (self):#all requested data is encapsulated in the requestconn =self.request Conn.sendall ('Welcome to Socketserver Server'. Encode ()) whileTrue:data= CONN.RECV (1024). Decode ()ifdata = ='Exit': Print('Disconnect from%s! '% (self.client_address,))#attention is a tuple! Break elifData:Print('data from%s client sent to you:%s'%(self.client_address, data)) Conn.sendall ('The server has received data'. Encode ())if __name__=='__main__': #Create a multithreaded TCP serverSock_server = Socketserver. Threadingtcpserver (('127.0.0.1', 8888), Mysockserver)Print('Start the server! ') #start the server and the server will remain runningSock_server.serve_forever ()
Analyze the server-side code, with these key points:
- The connection data is encapsulated in the "self". Request "Attribute! Through "Self. " The request object calls the Send () and recv () methods.
- The "Handle ()" method is the processing core of the entire communication, and once it runs, the current connection is disconnected (but the other threads and clients are OK), so an infinite loop is generally set here.
- Note "Sock_server = Socketserver. " Threadingtcpserver ((' 127.0.0.1 ', 8888), Mysockserver) "The method passed in the parameter.
- "Sock_server. Serve_forever () "means that the server will run forever under normal circumstances.
Client
ImportSocket#The client still uses the socket module and does not need to import the Socketserver moduleIp_port= ('127.0.0.1', 8888) sock=Socket.socket () sock.connect (Ip_port) sock.settimeout (0.5) Data= SOCK.RECV (1024). Decode ()Print('receive return Data:%s'%data) whileTRUE:INP= Input ('Enter the data you want to send:'). Strip ()if notINP:ContinueSock.sendall (Inp.encode ())ifINP = ='Exit': Print('thanks for using, goodbye! ') BreakData= SOCK.RECV (1024). Decode ()Print('receive return Data:%s'%data) Sock.close ()
The client code is well understood, still using the socket module is OK, do not need to import the Socketserver module.
So far.
Python Sockets Socketserver Network programming