Creating a simple Web page tutorial instance using the CGI module

Source: Internet
Author: User
The last few days have been learning Python web programming, the main content is to build a simple Web server and the use of CGI modules to build a simple Web page (the word is not professional enough to forgive).

There are three types of modules that Python uses to build HTTP server:
1) Basehttpserver: Provide basic Web services and processor classes, respectively, Httpserver and Basehttprequesthandler;
2) Simplehttpserver: Contains the Simplehttprequesthandler class that performs the get and head requests;
3) Cgihttpserver: Contains the Cgihttprequesthandler class that handles post requests and executions.

Python's simplest Web server looks like this:


So you can access the contents of the server


For example, direct access to the following HTML page results in the following:


The hello.html is stored in the root directory of the server and the code is as follows:

<meta http-equiv= ' content-type ' content= ' text/html; Charset=utf-8 ' >

In the hello.html file is linked to a hello_get.py, the file is stored in the server root directory under the Cgi-bin folder, the code is as follows:

#!/usr/bin/python# Import modules for CGI handlingimport CGI, cgitb# Create instance of fieldstorageform = CGI. Fieldstorage () # Get data from fieldsfirst_name = Form.getvalue (' first_name ') last_name  = Form.getvalue (' last_name ') ) print "content-type:text/html\r\n\r\n" print "

In the previous image of the page to enter information, click Submit, the results are as follows:


It's worth noting that I've been doing this for a long time, calling the Py file to either show a blank or go wrong. View a lot of network resources, found that the problem is the permissions of the Py file, only need to execute chmod 755 xxx.py. In addition

#!/usr/bin/python It is best not to have any other information, I found the above to protect the following information is not displayed
"' Created on 2015-1-12@author:root '

I am also just a beginner, can only say that my study process to share, to avoid people meet me the same problem and spend a lot of time.

"Recommended"

1. Detailed CGI writing data instance code to text or database

2. Sharing an example tutorial on using CGI to run Python scripts on IIS

3. What is CGI? Detailed description of Python CGI programming

4. Share an example tutorial on pythoncgi programming

5. Sample code for XML and modern CGI applications

6. The FastCGI process unexpectedly exits resulting in a 500 error

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.