I learned about the Web server section of Python core programming today. So a little bit of Python's source code.
I installed on the computer is python2.6, the corresponding source files are stored in the/usr/lib/python2.6/path.
1 Basehttpserver Analysis
Open the/usr/lib/python2.6/basehttpserver.py file.
1.1 Httpserver Class
The top defines the class Httpserver, inheriting from Socketserver.tcpserver, which continuously receives the data and gives the received data to RequestHandler processing.
It does not add a lot of functionality on the basis of tcpserver, only adds a server_bind () member function.
1.2 Basehttprequesthandler Class
Seeing the class Basehttprequesthandler, this class is responsible for handling incoming HTTP requests, such as Post,get.
See it's member function handle ()
Request processing is done by calling the handle () function. First, it resets the class member variable close_connection to 1, and if it is not set to 0 in Handle_one_request () execution, then handle () returns.
So under what circumstances will close_connection be set at 0? If there is connection:keep-alive in the requested header, it will be cleared 0. See Parse_request () in:
As you can see from the while loop above, if Close_connection is 0, continue to execute handle_one_request () until the close_connection is 1.
So what is handle_one_request () doing? As the name implies, it is processing a request.
L312: Reads the requested data from the Rfile, which is the HTTP message data.
l313~l315: If read fails to exit.
l316~l317: Call Parse_request () to parse the HTTP message header and exit if it fails.
L318: According to command, generate a handler function name, such as the GET command generates Do_get.
l319~l323: Checks whether the current class has a do_xxx () member function, if there is a do_xxx () member function.
Take a look at how Parse_request () parses the HTTP header. Two main steps:
(1) Read the first line of the message data in the format:< name > < path >
The analysis version is correct and resolves the command, path, version, and save to the corresponding member variable.
(2) Check headers is in the connection, if it is keep-alive, then you have to set the Close_connection to 0 to save the connection.
From the analysis of the Basehttprequesthandler, we can tell that if we want to respond to the post,get command, it must inherit from Basehttprequesthandler and define the Do_get () and Do_post () functions.
In addition to the three important functions mentioned above, Basehttprequesthandler also provides a number of useful member functions:
Send_error (Code, Message=none)
Send_respond (Code, Message=none)
Send_header (keyword, value)
End_headers ()
...
2 Cgihttpserver Analysis
Open the/usr/lib/python2.6/cgihttpserver.py file. The file only defines a Cgihttprequesthandler class that inherits from the Simplehttpserverhandler.
In fact, Simplehttprequesthandler is inherited from Basehttprequesthandler.
It implements the Do_post () function:
The meaning is very simple, if is CGI, then executes the CGI, otherwise the error.
So what's a CGI? Let's track the Is_cgi () function:
It looks very simple, that is, the file under the directory cgi_directories, which is considered a CGI file. Cgi_directories is defined in L89, that is, in the/cgi-bin or/htbin directory, which is considered CGI.
The _url_collapse_path_split (path) function is used for a regular path, preventing too many occurrences in the path. /occurrence of the anti-ask vulnerability.
So how do you execute the CGI? Let's keep up with the run_cgi () function.
Cond......
Basehttpserver and Cgihttpserver Study notes