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='Content-Type' content='text/html; charset=UTF-8'> <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>
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('first_name') last_name = form.getvalue('last_name') 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 id="Hello-nbsp-s-nbsp-s">Hello %s %s</h2>" % (first_name, last_name) print "</body>" print "</html>"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之前最好不要有其他信息,反正我发现上方保护如下信息的时候是显示不出来的
''' Created on 2015-1-12 @author: root '''
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】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!
Python and Time: Making the Most of Your Study TimeApr 14, 2025 am 12:02 AMTo maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.
Python: Games, GUIs, and MoreApr 13, 2025 am 12:14 AMPython excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.
Python vs. C : Applications and Use Cases ComparedApr 12, 2025 am 12:01 AMPython is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.
The 2-Hour Python Plan: A Realistic ApproachApr 11, 2025 am 12:04 AMYou can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.
Python: Exploring Its Primary ApplicationsApr 10, 2025 am 09:41 AMPython is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.
How Much Python Can You Learn in 2 Hours?Apr 09, 2025 pm 04:33 PMYou can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.
How to teach computer novice programming basics in project and problem-driven methods within 10 hours?Apr 02, 2025 am 07:18 AMHow to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...
How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?Apr 02, 2025 am 07:15 AMHow to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools







