Home Backend Development Python Tutorial Using CGI module to create a simple web page tutorial example

Using CGI module to create a simple web page tutorial example

May 19, 2017 pm 01:06 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHP and CGI to implement user registration and login functions How to use PHP and CGI to implement user registration and login functions Jul 21, 2023 pm 02:31 PM

How to use PHP and CGI to implement user registration and login functions User registration and login are one of the necessary functions for many websites. In this article, we will introduce how to use PHP and CGI to achieve these two functions. We'll demonstrate the entire process with a code example. 1. Implementation of the user registration function The user registration function allows new users to create an account and save their information to the database. The following is a code example to implement the user registration function: Create a database table First, we need to create a database table to store user information. Can

PHP and CGI file upload and download technology: how to implement file management functions PHP and CGI file upload and download technology: how to implement file management functions Jul 21, 2023 am 11:19 AM

File upload and download technology with PHP and CGI: How to implement file management functions Introduction: File upload and download are one of the common functions in modern web applications. This article will introduce how to implement file upload and download functions using PHP and CGI programming languages, and show some code examples to demonstrate how to manage uploaded and downloaded files. Here’s what we’re going to cover: Basic concepts of file upload using PHP to implement file upload CGI to implement file upload Basic concepts of file download using PHP to implement file download CGI implementation under the file

How to use PHP and CGI to implement the video playback function of the website How to use PHP and CGI to implement the video playback function of the website Jul 22, 2023 pm 07:45 PM

How to use PHP and CGI to implement the video playback function of the website In today's multimedia era, video has become an indispensable part of the website content. In order to provide a good user experience, the website needs to implement video playback function. This article will introduce how to use PHP and CGI to implement the video playback function of the website, and provide code samples for reference. 1. Preparation Before starting, you need to ensure that the server has PHP and CGI modules installed. You can do this by running the phpinfo() function or typing "php

Explore the working principles, similarities and differences between PHP CLI and CGI Explore the working principles, similarities and differences between PHP CLI and CGI Mar 11, 2024 pm 12:39 PM

Working principles and similarities and differences In web development, PHP is a commonly used programming language that can interact with web servers in different ways, the most common of which are through PHPCLI (CommandLineInterface) and PHPCGI (CommonGatewayInterface). This article will explore the working principles, similarities and differences between PHPCLI and CGI, and provide specific code examples to illustrate the differences between them. 1. PHP

PHP and CGI technologies compared: How to choose the right one for your website PHP and CGI technologies compared: How to choose the right one for your website Jul 22, 2023 am 09:45 AM

Comparison of PHP and CGI technologies: How to choose the right website for you With the development of the Internet, CGI (Common Gateway Interface) and PHP (Hypertext Preprocessor) have become one of the most commonly used website development technologies. This article will compare these two technologies to help you choose the right development technology for your website. 1. Overview PHP is a very popular server-side scripting language that is widely used for dynamic website development. It is open source, supports multiple operating systems, and has powerful database connection and processing capabilities. Developers can use simple syntax

The relationship between cgi, fast-cgi and php-fpm (with flow chart) The relationship between cgi, fast-cgi and php-fpm (with flow chart) Oct 08, 2022 pm 02:07 PM

CGI is a protocol and has nothing to do with the process. For example, when the web server (nginx) receives a PHP network request, nginx needs to find the PHP parser according to the configuration file. After simple processing, some of the requested information is handed over to The PHP parser stipulates which protocols are to be transmitted and the format in which they are transmitted. This standard is called the cgi protocol.

Template engines for PHP and CGI: How to achieve website reusability Template engines for PHP and CGI: How to achieve website reusability Jul 20, 2023 pm 10:13 PM

PHP and CGI template engines: How to achieve website reusability Introduction: When developing websites, we often need to deal with the display of dynamic content. In order to achieve code maintainability and reusability, using a template engine is a wise choice. This article will introduce PHP and CGI, two commonly used template engines, and show how to use them to achieve website reusability through code examples. 1. PHP template engine PHP is a widely used server scripting language with flexibility and powerful functions. PHP template engine is a

Using machine learning to reconstruct faces in videos Using machine learning to reconstruct faces in videos Apr 08, 2023 pm 07:21 PM

Translator | Reviewed by Cui Hao | Sun Shujuan begins with a collaborative study between China and the UK that has devised a new way to recreate faces in video. This technology can enlarge and reduce facial structure with high consistency and no trace of artificial trimming. Typically, this transformation of facial structure is achieved through traditional CGI methods, which rely on detailed and expensive motion capping, rigging and texturing procedures to completely reconstruct the face. Unlike traditional methods, CGI in the new technology is integrated into the neural pipeline as a parameter for 3D facial information and serves as the basis for the machine learning workflow. The author points out: “Our goal is to refine facial contours based on natural faces in the real world.

See all articles