Using Python's socketserver framework to write a network service program __python

Source: Internet
Author: User

1. Preface:

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

2. Network Service category:

The 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 relationship is as follows:

+------------+
| Baseserver |
+------------+
      |
      V
+-----------+ +        ------------------+
| TCPServer |------->| Unixstreamserver |
+-----------+        +------------------+
      |
      V
+-----------+ +        --------------------+
| Udpserver |------->| Unixdatagramserver |
+-----------+        +--------------------+

2.1 Asynchronous Processing:

This four service classes are all synchronous processing requests. A request was not processed and the next request could not be processed. To support the 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 (forking) to implement Asynchrony.

ThreadingMixIn uses multithreading to implement Asynchrony.

3. Request Processing class:

To implement a service, you must also 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 requests through a combination of service classes and request processing classes.

The request processing class provided by the Socketserver module has Baserequesthandler, as well as its derived class Streamrequesthandler and Datagramrequesthandler. It can be seen from the name that one handles the streaming socket and one handles the datagram socket.

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

1. Create a request handler class (requests processing classes), inherit from Baserequesthandler class and override its handle () method, which will be processed to the request.
2. Instantiate a server class object and pass it to the address of the service and the request handler class that was created previously.
3. Call the Handle_request () or Serve_forever () method of the server class object to begin processing the request.


An example of an socketserver server: [plain] view plain copy print? From Socketserver import Tcpserver,streamrequesthandler #定义请求处理类 class Handler (streamrequesthandler): Def Han Dle (self): addr = Self.request.getpeername () print ' Got connection from ', addr self.wfile.wr ITE (' Thank for connecting ') Server = TCPServer ((', 1234), handler) #实例化服务类对象 Server.server_forever () #开启服务


5. Implement asynchronous, support multiple connections

As mentioned in the previous introduction of the service class, the four basic service classes default to the synchronization model. To support Asynchrony, you can use multiple inheritance to define a service class that supports asynchrony from the forkingmixin or threadingmixinmix-in classes and a basic service class inheritance. Like what:

Class Server (ThreadingMixIn, tcpserver): Pass

        

Forkingmixin to consider the communication between processes. ThreadingMixIn to consider synchronization and mutual exclusion when a thread accesses the same variable.

An example of a server that uses multithreaded processing:[Plain]View Plain copy print? From Socketserver import TCPServer, ThreadingMixIn, Streamrequesthandler #定义支持多线程的服务类, note that multiple inheritance class Server (Threading  Mixin, TCPServer): Pass #定义请求处理类 class Handler (Streamrequesthandler): def handle (self): addr = Self.request.getpeername () print ' Got connection from ', addr self.wfile.write (' Thank Your for Connec tion ') Server = Server ((', 1234), Handler) #实例化服务类 server.serve_forever () #开启服务


6. For reference:

The Python Basics Tutorial (2nd edition)

Python online documentation http://docs.python.org/2/library/socketserver.html

Related Article

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.