Python built-in standard library socketserver module, pythonsocketserver

Source: Internet
Author: User

Python built-in standard library socketserver module, pythonsocketserver

The socketserver module simplifies the compilation of network server tasks and encapsulates some operations to a large extent. You can think of it as an event-driven design, which is quite good. It defines two basic classes: server class BaseServer and request processing class BaseRequestHandler.

BaseServer basic server class encapsulates some basic socket operations. The socket-related operations in the socket primitive are only encapsulated in the accept method. The operations before this method are also encapsulated, And the next send, the recv primitive operation is not being encapsulated, so where does it go? This is a good design of the socketserver module. socketserver not only operates on socket primitives, but also encapsulates the base classes that require specific processing, that is, the BaseRequestHandler class. take two examples, the first is the processing of WSGI protocol, see the specific <a simple introduction to the web server and python application between the http://www.cnblogs.com/zhiyong-ITNote/p/7522093.html>. find the wsgiref folder in the python installation directory, and analyze the simple_server.py module and handlers. py module.
Note the make_server function of simple_server.py. The fifth parameter of this function is the custom request processing class. We use this class to process the WSGI protocol, to implement communication between the server and the python application. let's take a look at the inheritance sequence of this class:
WSGIRequestHandler → BaseHTTPRequestHandler → StreamRequestHandler → BaseRequestHandler.
Let's look at the implementation of the final parent class:

class BaseRequestHandler:"""Base class for request handler classes.This class is instantiated for each request to be handled. Theconstructor sets the instance variables request, client_addressand server, and then calls the handle() method. To implement aspecific service, all you need to do is to derive a class whichdefines a handle() method.The handle() method can find the request as self.request, theclient address as self.client_address, and the server (in case itneeds access to per-server information) as self.server. Since aseparate instance is created for each request, the handle() methodcan define other arbitrary instance variables."""def __init__(self, request, client_address, server):self.request = requestself.client_address = client_addressself.server = serverself.setup()try:self.handle()finally:self.finish()def setup(self):passdef handle(self):passdef finish(self):pass

Note the explanation of the handle () function in the annotations. This function is used to actually process each request. return to the WSGIRequestHandler program. The WSGIRequestHandler class has a handle () function. This function is used to override and implement the base class handle () function, which divides the request processing into handlers. this module is used to process the WSGI protocol.

The second example is the python built-in HTTPServer. We start the built-in HTTPServer in python 3 and input the following in the command line:
Python-m http. server 8000
Look at the browser's response:

Open the http folder under the python installation directory. Let's take a look at the server. py file. Then let's look at the code of the test () function and find the HandlerClass variable. It points to the SimpleHTTPRequestHandler class. Let's look at the inheritance sequence of this class:
SimpleHTTPRequestHandler → BaseHTTPRequestHandler → StreamRequestHandler → BaseRequestHandler
The third class is used to process TCP streaming communication. We can see that the HTTP server's request processing is based on the BaseRequestHandler class, and the handle () function is used to process the request, the handle () function in the BaseHTTPRequestHandler class distributes the request processing tasks to other functions.

Finally, let's make a summary. The two built-in implementations of python are based on socketserver. the py module calls the BaseServer class and BaseRequestHandler class. The former encapsulates basic socket processing, and the latter encapsulates request processing through the handle () the function distributes request processing to other specific processes.

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.