Using Python's socketserver framework to write Network service programs

Source: Internet
Author: User
1. Preface:

Although it is convenient to write simple web programs in Python, it is better to use a ready-made framework for a complex network program. This allows you to focus on the transaction logic rather than the various details of the socket. The Socketserver module simplifies the task of writing Network service programs. The Socketserver module is also the basis for many of the server frameworks in the Python standard library.

2. Network SERVICE class:

Socketserver provides 4 basic service classes:

TCPServer for TCP sockets stream

Udpserver for UDP datagram sockets

Unixstreamserver and Unixdatagramserver 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:

The four service classes are synchronous processing requests. One request does not finish processing the next request. To support an asynchronous model, you can take advantage of multiple inheritance to let the server class inherit Forkingmixin or ThreadingMixIn mix-in classes.

Forkingmixin uses multiple processes (forks) to achieve asynchrony.

ThreadingMixIn uses multithreading to implement Asynchrony.

3. Request Processing class:

To implement a service, you also have to derive a handler class request handler and override the handle () method of the parent class. The handle method is used specifically to process requests. The module handles the request through a combination of service classes and request processing classes.

The Socketserver module provides a request processing class with Baserequesthandler, as well as its derived classes Streamrequesthandler and Datagramrequesthandler. From the name can be a processing stream socket, a processing datagram socket.

4. Summarize the steps to create a service with Socketserver:

1. Create a Request handler class (Request processing Class), inherit from the Baserequesthandler class and override its handle () method, which will process the requests to the method.

2. Instantiate a server class object and pass the address of the service to it with the request handler class you created earlier.

3. Call the Handle_request () or Serve_forever () method of the server class object to begin processing the request.

An example of a server based on Socketserver:

From socketserver import tcpserver,streamrequesthandler# definition Request processing Class Handler (Streamrequesthandler):d EF handle (self) : addr = self.request.getpeername () print ' Got connection from ', Addrself.wfile.write (' Thank-for-connecting ') Server = T Cpserver ((' ', 1234), handler) #实例化服务类对象server. Server_forever () #开启服务

5. Implement asynchronous, support multi-connection

As mentioned in the previous service class, four basic service classes are synchronous models by default. To support asynchrony you can take advantage of multiple inheritance from Forkingmixin or threadingmixinmix-in classes and a basic service class inheritance to define a service class that supports Asynchrony. Like what:

Class Server (ThreadingMixIn, tcpserver): Pass

Forkingmixin to consider inter-process communication. ThreadingMixIn to consider synchronization and mutual exclusion when threads access the same variable.

An example of a server that uses multithreaded processing:

From Socketserver import TCPServer, ThreadingMixIn, streamrequesthandler# defines a service class that supports multithreading, note that multiple inheritance of class Server ( ThreadingMixIn, tcpserver): pass# definition Request processing Class Handler (Streamrequesthandler): def handle (self): addr = Self.request.getpeername () print ' Got connection from ', Addrself.wfile.write (' Thank-for-connection ') Server = Server ( (', 1234), Handler) #实例化服务类server. Serve_forever () #开启服务
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.