1. Preface:
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 |------->| UnixDatagramServer |+-----------+ +--------------------+
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 asynchronous models, you can use multi-inheritance to inherit server classes.ForkingmixinOrThreadingmixinMix-in
Classes.
ForkingmixinAsynchronous implementation using multiple processes (forks.
ThreadingmixinAsynchronous implementation using multiple threads.
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, its derived class StreamRequestHandler andExtends ramrequesthandler. 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 multiple inheritanceForkingmixinOrThreadingmixinMix-in
ClassesAnd a basic service class inheritance to define a service class that supports Asynchronization. For example:
Class server (threadingmixin, tcpserver): Pass
ForkingmixinInter-process communication should be considered.ThreadingmixinConsider synchronization and mutex 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
6. refer:
Basic Python tutorial (version 2nd)
Python online document http://docs.python.org/2/library/socketserver.html