Basehttprequesthandler handle () Handle_one_request () method ()

Source: Internet
Author: User
Tags class definition error handling response code
Python standard library source analysis: basehttpserver.py

It is known from socketserver.py analysis that the design idea is to divide the listening loop of the socket programming and the client processing into the server class and the RequestHandler class, and the Basehttpserver is based on the socketserver basis, so you can know the BAS Ehttpserver is to extend the server class and the RequestHandler class separately. Basehttpserver implementation of a simple HTTP Server, you can know that the main work should be extended requesthandler function, processing the HTTP request of the client, including HTTP protocol resolution, to the client return HTTP response, logging and other functions. 1.HTTPServer

The class is simply packaged in the Socketserver Tcpserver[1] class 2.BaseHTTPRequestHandler

File "/usr/local/lib/python2.6/threading.py", line 504, in __bootstrap
Self.__bootstrap_inner ()
File "/usr/local/lib/python2.6/threading.py", line 532, in __bootstrap_inner
Self.run ()
File "/usr/local/lib/python2.6/threading.py", line 484, in run
Self.__target (*self.__args, **self.__kwargs)
File "/usr/local/lib/python2.6/socketserver.py", line 560, in Process_request_thread
self.finish_request (Request, client_address)
File "/usr/local/lib/python2.6/socketserver.py", line 322, in Finish_request
self. Requesthandlerclass (Request, client_address, self)
File "/usr/local/lib/python2.6/socketserver.py", line 617, in __init__
Self.handle ()
File "/usr/local/lib/python2.6/basehttpserver.py", line 329, in handle
Self.handle_one_request ()
File "/usr/local/lib/python2.6/basehttpserver.py", line 323, in Handle_one_request
Method ()

Class definition

Class Basehttprequesthandler (Socketserver.streamrequesthandler):

Extension RequestHandler need to overwrite an interface handler (), defined as follows:

def handle (self): "" "Handle multiple requests if necessary." "" Self.close_connection = 1 Self . Handle_one_request ()While not self.close_connection:self.handle_one_request ()

The main deal is whether HTTP remains connected to the problem, if you keep the connection to continue processing customer requests, otherwise it is over.
Handle_one_request () More important statements:

Self.raw_requestline = Self.rfile.readline (65537) self.parse_request ()Mname = ' Do_ ' + Self.command if not hasattr (self, Mname) self.send_error (501, "Unsupported Method (%r)"% Self.command) re Turn Method = GetAttr (self, mname) Method ()Self.wfile.flush () #actually Send the response if not already done.

Read the first line of Raw_requestline, the general format should be: COMMAND PATH version\r\n; Parse the request, the following concrete analysis; The following lines of code mean to get the corresponding handler function based on the HTTP method. In fact, the corresponding do_get () or Do_post () method is invoked based on a GET or POST request, and then the output is refreshed.
Parse_request () The main code is in the process of raw_requestline, eventually get Self.command, Self.path, self.request_version several variables, and then is the use of mimetools. The message resolves the head.
This is the end of the main process.
Here are a few more functions that are simpler:

def send_error (self, Code, Message=none): Def send_response (self, Code, Message=none): Def send_header (self, keyword, val UE): def end_headers (self): def log_request (self, code= '-', size= '-'): Def log_error (self, format, *args): Def log_message (Self, format, *args):

Summary: you can see that Basehttprequesthandler mainly implements client-side HTTP request resolution, as well as some accessibility features such as logging, error handling, sending response code, and so on. However, the implementation of HTTP requests has not yet been implemented, simply by implementing functions such as Do_get (), Do_post (), and the corresponding HTTP commands.

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.