Python socketserver framework parsing, pythonsocketserver
The socketserver framework is a basic socket server framework. It uses threading to process connections between multiple clients and the seletor module to handle high-concurrency access, it is one of the source code of the python standard library.
For more information about the select network framework, see <python select. select module communication process details>. The socketserver framework uses the selector framework for you to select suitable network communication frameworks, such as select, poll, and epoll. With these network frameworks, we can handle highly concurrent network access. Let's take a look at the sample code:
# Coding: The utf-8import socketserverclass MyTCPHandler (socketserver. baseRequestHandler): "" The request handler class for our server. it is instantiated once per connection to the server, and must override the handle () method to implement communication to the client. "def handle (self): # self. request is the TCP socket connected to the client self. data = self. request. recv (1, 1024 ). strip () print ("{} wrote :". format (self. client_address [0]) print (self. data) # just send back the same data, but upper-cased self. request. sendall (self. data. upper () if _ name _ = "_ main _": HOST, PORT = "localhost", 9999 # Create the server, binding to localhost on port 9999 # If the subclass does not have a method or attribute, go back to the parent class and call with socketserver. threadingTCPServer (HOST, PORT), MyTCPHandler) as server: # Activate the server; this will keep running until you # interrupt the program with Ctrl-C server. serve_forever ()
Client:
# Coding: utf-8import socketsk = socket. socket () sk. connect ("127.0.0.1", 9999) # actively initialize the connection with the server while True: send_data = input ("input sent content:") sk. sendall (bytes (send_data, encoding = "utf8") if send_data = "byebye": break accept_data = str (sk. recv (1024), encoding = "utf8") print ("". join ("received content:", accept_data) sk. close ()
We create a TCP request processing class inherited from the BaseRequestHandler class. To put it bluntly, this class is our own socket-based recv () function and send () function class, the so-called TCP request processing class is actually an encapsulation class that processes the bind, listen, and accept of the socket server, and this encapsulation is not a simple socket, it is based on the select or epoll network framework class. We can easily handle high concurrency network access by calling this class. in fact, read the source code carefully and you will find that the overall program design is event-driven. There are three elements of the event-driven mechanism: Message (event) queue and message (event) triggering, event loop. Only the event-driven mechanism of the socketserver framework implements the socket accept () method, and the next message acceptance (recv) and sending (send) it is not encapsulated into corresponding events for processing.
I use the ThreadingTCPServer () class to respond to connections from multiple clients. But when I read the source code of this class, I am confused!
class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass
This made me very confused. Let's look at the call:
with socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler) as server: # Activate the server; this will keep running until you # interrupt the program with Ctrl-C server.serve_forever()
This is even more confusing, where is the ThreadingTCPServer constructor? Later I studied it. If a function or attribute of a subclass is called, if the method or attribute of the parent class is not overloaded, if this method or attribute is not in the subclass, the method or attribute corresponding to the parent class will be called, therefore, the initialization functions _ init _ and serve_forever () of the TCPServer are called, and then the Request_handler function of the subclass is called.
Program result: