existPython Exploration of SocketServer DetailsIn SocketServer, we introduced the SocketServer module of the Python standard library and learned that to implement a network communication service, you need to build a server class and a request processing class. The module also creates different server classes and request processing classes for us.
1. Server class
BaseServer
TCPServer(BaseServer)
UDPServer(TCPServer)
UnixStreamServer
UnixDatagramServer
2. Request processing class
BaseRequestHandler
StreamRequestHandler(BaseRequestHandler)
DatagramRequestHandler(BaseRequestHandler)
Through the matching of server classes and request handling classes, we can create different types of servers and implement different protocol types. This article introduces the BaseHTTPServer module is to inherit TCPServer and StreamRequestHandler, the implementation of the Web server communication.
HTTP server
The HTTP server inherits from the TCPServer class in the SocketServer module. It has a very simple definition and just overrides one of its methods.
class HTTPServer(): allow_reuse_address = 1 # Seems to make sense in testing environment def server_bind(self): """Override server_bind to store the server name.""" .server_bind(self) host, port = ()[:2] self.server_name = (host) self.server_port = port
The rewritten server_bind() method is mainly to get the server name and port. The rest of the methods and the server implementation are detailed inPython Exploration of SocketServer Details
It is also possible to create servers that support processes or threads based on HTTPServer by introducing the 'mix-in' class from the SocketServer module.
HTTP request processing base class
To handle HTTP requests, the BaseHTTPServer module constructs the HTTP request handling base class BaseHTTPRequestHandler, which inherits from the StreamRequestHandler class in the SocketServer module.
There are some important methods in the HTTP request processing base class:
1.handle() --This method is the method where the request handling class actually handles the request specific work, such as parsing the incoming request, processing the data, and sending back the response. In BaseHTTPRequestHandler it is an entry file that will call other methods to complete the request processing.
2.handle_one_request() --Called by handle() to process the request. Its main jobs include:
Calls the parse_request() method to parse the request and get the information in the request message, including the requested method, the request URL, the requested HTTP version number, the request header, and so on. If parsing fails, call the send_error() method to send back an error response.
Call the do_SPAM() method. The SPAM in this method refers to request methods such as GET, POST, HEAD, etc. You need to build the specific request processing methods in the request processing class, for example, do_GET for GET requests, do_POST for POST requests. do_SPAM() method can be called to send_response(), send_header(), end_headers() methods to create content such as the first line of the response and the first part of the response.
3.parse_request() --Parses the request.
4.send_error() --Send back an error response.
5.send_response() --Creates the response first line, response header, etc.
6.send_header() --Sets the response header content.
7.end_headers() --Call this method to add a blank line after the header to indicate the end of the header content (not applicable to HTTP/0.9)
8. Also includes a number of other auxiliary functions.
Something to keep in mind:BaseHTTPRequestHandleris the base class for HTTP request processing, and does not contain methods such as do_GET, do_POST, etc. Other request processing classes inheriting this class need to implement these methods on their own to complete the processing of specific requests. For this, see the SimpleHTTPServer module, or the articlePython exploration of the implementation of a simple HTTP server。
summarize
The above is the full content of this article on the python exploration of BaseHTTPServer-implementation of the Web server introduction, I hope it will help you. Interested friends can continue to refer to this site:Python Exploration of the URL Dispatcher example details、Introduction to functions under the Re module of Python Programmingetc., if there are deficiencies, welcome to leave a message to point out. Thank you friends for the support of this site!