Python Learning Note 20: Server advanced

Source: Internet
Author: User

Previous we have completed a Python server that can handle HTTP requests without relying on frames and CGI, using only the socket interface.
Based on this, any computer with an operating system (Linux recommended) and Python can be used as an HTTP server to set up your website.

We will be here to rewrite the program in the previous article and introduce more advanced Python packages to write more mature python servers.


A support post we first rewrite the HTTP server in the original source, allowing the server to support richer HTTP requests.
In relation to the original program, the table is added here and the corresponding "POST" method is manipulated. If you've read a Python server with a socket, you'll find that it's just a little bit more.
Original program:

# A Messy HTTP server based on TCP socket import Socket # addresshost = ' PORT = 8000 text_content = ' ' http/1.x OK Content-type:text/html 

Run the above Python server and, as an article, use a browser as a client.
We saw the new form and the Submit button. Enter AA in the table and submit the table, and our Python server gives the results above.

Two use socketserver we first use the Socketserver package to simplify the process of setting up our servers.
In the process of using the socket above, we first set the socket type, then call bind (), listen (), accept (), and use the while loop to keep the server accepting requests.
The above steps can be simplified by socketserver packages.

socketserver:# use tcpserver import socketserver HOST = ' PORT = 8000 text_content = ' ' http/1.x OK Content-type:tex T/html 
We created a TCPServer object to create a TCP socket server and set the IP address and port at the same time.
Then use the Server_forever () method to keep the server working (just like the while loop in the original program).
We pass to tcpserver a Mytcphandler class, using the socket to make the operation.
Note that Mytcphandler inherits from Baserequesthandler, and we personalize our operations by rewriting the handler () method.
In handler (), the socket can be referenced by self.request (as we do with the socket recv () and Sendall () in Handler ()),
You can also use self.address to refer to the client address of the socket.
Three simplehttpserver: Using a static file to respond to a request after a socketserver transformation, our handler (), which is the part that handles the request, remains a messy mess.
This can be a problem for large servers.
Why is it? For an HTTP request (request), its starting line contains two important information: the request method and the URL.
Before, we used the if structure to distinguish between different request methods and URLs, and to do different things for different situations:


Request method URL Action


GET/Send Text_content


Get/text.jpg Send Pic_content


POST/analyze the value contained in the request body (which is actually what we fill in the table); Send Text_content and value

Depending on the request method and URL, a large HTTP server may need to cope with thousands of different requests.
It takes a lot of time and effort to write different actions in the program for each request, and it can be difficult to operate and maintain.
We need to have a more standardized and easier way to handle these requests.
In Python, we can use the Simplehttpserver package and the Cgihttpserver package to reduce the burden.
Where simplehttpserver can be used to handle requests for the Get method and head method.
It reads the URL address in the request, finds the corresponding static file in the current directory, and sends the contents of the file to the client.
In our case, the text_content is placed in the index.html without having to read the Text.jpg file.
When an HTTP request arrives, its URL points to a file, Simplehttpserver reads the file, parses the file type, automatically generates response, and responds to the client.
If the URL points to a folder, Simplehttpserver reads the index.html or INDEX.HML file under that folder.
First, we generate the following index.html file in the current directory:


Then, rewrite our Python server program.
In fact, we just replaced the TCPServer handler: using the only class Simplehttprequesthandler in the Simplehttpserver package, not the mytcphandler we defined earlier

simplehttpserver:# Simple httpserver Import socketserverimport simplehttpserver HOST = ' PORT = 8000 # Create the server, Simplehttprequesthander is pre-defined handler in simplehttpserver Packageserver = Socketserver.tcpserver ((HOST, PORT), Simplehttpserver.simplehttprequesthandler) # Start the Serverserver.serve_forever ()


Note that our program here is not equivalent to the previous program, because the POST request cannot be processed. We will use CGI later to compensate for this flaw.
But the point is that our Python server program has become very simple.
We separate the content from the Python server by storing the content in a static file and providing the content to the client based on the static file that the URL points to.
In this case, we can only modify the static file each time we update the content, instead of stopping the entire Python server.
We should also pay attention to the cost of using these improvements. For example, for the original program, the URL in request is only instructive, we can arbitrarily specify the corresponding operation.
In Simplehttpserver's improvement, response is cured by reading the URL file and presenting its contents to the customer. This greatly limits our degrees of freedom.
Even after we used CGI to increase our degrees of freedom, we added our own limitations relative to the original program.
Sometimes, the convenience of the program contradicts the freedom of the program, and the programmer needs to choose between the two.
For a small project, we can follow established standards (such as simplehttpserver here, or use a framework), and using these new standards can make development easy.
For a large project, however, we often need to get back to our own freedom and revise the criteria needed for the project.

Quad Cgihttpserver: Use static files or CGI to respond to requests the Cgihttprequesthandler class in the Cgihttpserver package inherits from the Simplehttprequesthandler class, So it can be used instead of the example above to provide a service for static files.
In addition, the Cgihttprequesthandler class can also be used to run CGI scripts.

First, let's look at what is CGI (Common Gateway Interface). CGI is a set of interface standards between server and application scripts, which is intended to allow the server program to run a script that sends the output of the program as response to the customer.
Typically, a CGI-enabled server runs the corresponding script file, based on the URL in the request, after receiving the request from the client.
The server enters the HTTP request information and socket information into the script file, collects the output of the script, and assembles it into a legitimate HTTP response.
Using CGI, we can make full use of the server programmability, dynamically generate response, without having to be confined to static files.
The CGI standard is used as an interface between the server and the CGI script. This allows the server to work with CGI scripts written in different languages, such as CGI script written by Apache server and Perl, or CGI scripts written by the Python server and shell.
So far, we are using TCPServer to build the server. In order to use CGI, we need to build the server using the Httpserver class in the Basehttpserver package.
In fact, Httpserver is a subclass of TCPServer, which uses the same method as TCPServer. It just adds two properties for server_name and Server_port.
But unfortunately, our cgihttprequesthandler needs to call these two properties ...
The Python server changes are simple.

cgihttpserver:# A Messy HTTP server based on TCP socket  import basehttpserverimport cgihttpserver HOST = ' PORT = 8000 # Create The server, Cgihttprequesthandler is pre-defined handlerserver = Basehttpserver.httpserver (HOST, PORT), cgihttp Server.cgihttprequesthandler) # Start the Serverserver.serve_forever ()

Cgihttprequesthandler the files in the Cgi-bin and Ht-bin folders in the default current directory are CGI scripts, and files stored elsewhere are considered static files.
Therefore, we need to modify the index.html to change the action where the form element points to cgi-bin/post.py.



We create a Cgi-bin folder and put the following post.py file in the Cgi-bin, which is our CGI script:


#!/usr/bin/env Python # written by vameiimport Cgiform = CGI. Fieldstorage () # Output to stdout, Cgihttpserver would take this as response to the Clientprint "content-type:text/html" 
   # HTML is followingprint                               # blank line, end of Headersprint "<p>hello world!</p>"         # Start of content Print "<p>" +  repr (form[' firstname ') + "</p>"


The first line must be in order to tell the Python server the language in which the script is used (our CGI is Python, and of course it can be another language, like bash).

The CGI package is used to extract the tabular information submitted in the request (we do not dive into the CGI package for a moment). The script is only responsible for outputting all the results to the standard output (using print).
The Cgihttprequesthandler collects these outputs and assembles them into response for delivery to the client.
If a request is a POST method, its URL must point to a CGI script (that is, a file in Cgi-bin or Ht-bin).
Cgihttprequesthandler inherits from Simplehttprequesthandler, so it can also handle requests for the Get method and head method.
At this point, if the URL points to a CGI script, the server transmits the result of the script to the client, and when the URL points to a static file, the server transfers the contents of the file to the client.
We can let CGI scripts perform database operations, such as putting received data into a database, and richer program operations.
CGI scripts provide the function of PHP in the lamp architecture (our Python server is equivalent to Apache in lamp).

Python Learning Note 20: Server advanced

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.