Although it is very convenient to write simple network programs in Python, it is better to use a ready-made framework for complicated network programs. 1:
Although it is very convenient to write simple network programs in Python, it is better to use a ready-made framework for complicated network programs. In this way, you can concentrate on the transaction logic, rather than the socket details. The SocketServer module simplifies the preparation of network service programs. The SocketServer module is also the basis of many server frameworks in the Python standard library.
2. network service class:
SocketServer provides four basic services:
TCPServer for TCP socket stream
UDPServer for UDP datagram socket
UnixStreamServer and unixw.ramserver are not commonly used for UNIX domain sockets.
Their inheritance relationships are as follows:
+ ------------ +
| BaseServer |
+ ------------ +
|
V
+ ----------- ++ ------------------ +
| TCPServer | -------> | UnixStreamServer |
+ ----------- ++ ------------------ +
|
V
+ ----------- ++ -------------------- +
| UDPServer | -------> | unix?ramserver |
+ ----------- ++ -------------------- +
2.1 asynchronous processing:
All the four service classes process requests synchronously. The next request cannot be processed after a request is processed. To support the asynchronous model, you can use multiple inheritance to make the server class inherit ForkingMixIn or ThreadingMixIn mix-in classes.
ForkingMixIn uses multiple processes (forks) to implement asynchronization.
ThreadingMixIn uses multithreading to implement asynchronization.
3. request processing class:
To implement a service, you must also derive a handler class request processing class and override the handle () method of the parent class. The handle method is used to process requests. This module processes requests by combining service classes and request processing classes.
The SocketServer module provides the following request processing classes: BaseRequestHandler and its derived classes StreamRequestHandler and DatagramRequestHandler. It can be seen from the name that a processing stream socket, a processing datagram socket.
4. summarize the steps for creating a service with SocketServer:
1. create a request handler class (request processing class) that inherits from the BaseRequestHandler class and overwrites its handle () method. this method will process the request.
2. instantiate a server class object and pass the service address and the previously created request handler class to it.
3. call the handle_request () or serve_forever () method of the server class object to start processing the request.
Example of a SocketServer-based server:
From SocketServer import TCPServer, StreamRequestHandler # defines the request processing class Handler (StreamRequestHandler): def handle (self): addr = self. request. getpeername () print 'got connection from', addrself. wfile. write ('thank you for connection') server = TCPServer ('', 1234), handler) # instantiate the service class Object server. server_forever () # enable the service
5. implement asynchronization and support multiple connections
As mentioned above, the four basic services are synchronous models by default. To support asynchronization, you can use multi-inheritance to define a service class that supports asynchronization from ForkingMixIn or ThreadingMixInmix-in classes and a basic service class inheritance. For example:
Class Server (ThreadingMixIn, TCPServer): pass
For ForkingMixIn, inter-process communication should be considered. ThreadingMixIn must consider synchronization and mutual exclusion when the thread accesses the same variable.
An example of a server with multi-threaded processing:
From SocketServer import TCPServer, ThreadingMixIn, StreamRequestHandler # defines a service class that supports multithreading. Note that it inherits multiple class servers (ThreadingMixIn, TCPServer): pass # defines the request processing class Handler (StreamRequestHandler ): def handle (self): addr = self. request. getpeername () print 'got connection from', addrself. wfile. write ('thank you for connection') server = Server ('', 1234), Handler) # instantiate the service server. serve_forever () # enable the service