Using CGI module to create a simple web page tutorial example

零下一度
Release: 2017-05-19 13:06:49
Original
2703 people have browsed it

I have been learning python web programming in the past few days. The main content is to build a simple web server and use CGI modules to create simple web pages (Sorry for the unprofessional use of words).

Python has the following three modules for building http server:
1) BaseHTTPServer: Provides basic Web services and processor classes, namely HTTPServer and BaseHTTPRequestHandler;
2) SimpleHTTPServer: Contains the SimpleHTTPRequestHandler class that performs GET and HEAD requests;
3) CGIHTTPServer: Contains the CGIHTTPRequestHandler class that handles POST requests and executes.

python The simplest web server is shown below:


##So you can access the content in the server


For example, access the following html page directly, the results are as follows:


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

<meta http-equiv=&#39;Content-Type&#39; content=&#39;text/html; charset=UTF-8&#39;>
<html><head><title>数据库列表</title></head>
<body>
<form action="/cgi-bin/hello_get.py" method="get">
First Name: <input type="text" name="first_name">  <br />

Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>
</body></html>
Copy after login

in hello.html There is a hello_get.py linked in the file, which is stored in the cgi-bin folder in the server root directory. The code is as follows:

#!/usr/bin/python

# Import modules for CGI handling
import cgi, cgitb

# Create instance of FieldStorage
form = cgi.FieldStorage()

# Get data from fields
first_name = form.getvalue(&#39;first_name&#39;)
last_name  = form.getvalue(&#39;last_name&#39;)

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Hello - Second CGI Program</title>"
print "</head>"
print "<body>"
print "<h2>Hello %s %s</h2>" % (first_name, last_name)
print "</body>"
print "</html>"
Copy after login

Enter the information on the page in the previous picture and click submit. The results are as follows:


It is worth noting: 1. I did it for a long time at first, but when calling the py file, it either showed blank or an error occurred. After checking a lot of network resources, I found that the problem was caused by the permissions of the py file. You only need to execute chmod 755 XXX.py. In addition

#!/usr/bin/python之前最好不要有其他信息,反正我发现上方保护如下信息的时候是显示不出来的
Copy after login
&#39;&#39;&#39;
Created on 2015-1-12


@author: root
&#39;&#39;&#39;
Copy after login

I am just a beginner, so I can only share my learning process to avoid everyone encountering the same problem as me and spending a lot of time.

【Related recommendations】


1.

Detailed explanation of cgi writing data example code to text or database

2.

Share an example tutorial on running Python scripts using CGI on IIS

3.

What is CGI? Detailed introduction to Python CGI programming

4.

Share an example tutorial of Python CGI programming

5.

Detailed explanation of XML and sample code of modern CGI applications

6.

FastCGI process unexpectedly exited causing 500 error

The above is the detailed content of Using CGI module to create a simple web page tutorial example. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!