Home Backend Development Python Tutorial Share an example tutorial of PythonCGI programming

Share an example tutorial of PythonCGI programming

May 19, 2017 pm 01:08 PM

In the past week, I continued to learn Python online and came into contact with CGI programming in Python. The official definition of CGI is as follows:

CGI (Common Gateway Interface), a common gateway interface, is a program that runs on The server, such as: HTTP server, provides an interface with the client HTML page.

I am more interested in how powerful applications Python CGI can write, so I started to learn the programming part of Python directly. First, I need to configure the web server to support CGI. My computer has already The wamp integrated development environment is installed, so there is no need to install Apache. There are many blog posts on the Internet about how to configure under Apache, and they are in the Linux environment, but I think the content of the configuration file should not be There will be big changes, so I started my tinkering journey. It turns out that using Windows for development is really a pitfall.

According to the online learning website Python CGI, you must first set up the CGI directory and modify httpd.conf in the Apache configuration file

ScriptAlias /cgi-bin/ /var/www/cgi-bin/
Copy after login

and thenModify the httpd.conf file as follows:

<Directory "/var/www/cgi-bin">
   AllowOverride None
   Options +ExecCGI
   Order allow,deny
   Allow from all</Directory>
Copy after login

Then add the .py suffix in AddHandler so that we can access .py The python script file at the end:

After doing all this, restart the server (Apache/Wamp) and write the first CGI program. The code is as follows:

test1.py

#!D:\Python27\python # -*- coding: UTF-8 -*-print "Content-type:text/html\r\n\r\n"print &#39;<html>&#39;print &#39;<head>&#39;print &#39;<title>Hello Word - First CGI Program</title>&#39;print &#39;</head>&#39;print &#39;<body>&#39;print &#39;<h2>Hello Word! This is my first CGI program</h2>&#39;print &#39;</body>&#39;print &#39;</html>&#39;
Copy after login

The content of the first line specifies the location of the script interpreter, which can be changed according to the path of your own installation

Then enter in the browser address bar: http://localhost/cgi-bin/test1.py

Then the browser access displays the following results:

Hello Word! This is my first CGI program
Copy after login

No problem

Then write a second program: hello_get.py

#!D:\Python27\python# -*- coding: UTF-8 -*-# CGI处理模块import cgi, cgitb 
# 创建 FieldStorage 的实例化form = cgi.FieldStorage() # 获取数据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 localhost/cgi-bin/hello_get.py?first_name=ZARA&last_name=ALI

However Instead of outputting

##Hello ZARA ALI

as stated on the website, the following error message appears


So I was puzzled. The configuration environment was the same and the codes were exactly the same. What was the problem? Then I searched for the Apache CGI configuration method under Windows, but the results were all under Linux. configuration method, so I had no choice but to reinstall wamp and then reconfigure the CGI environment. Then I did each of the above steps again, and then copied the code on the website to the

editor Then enter again in the browser:

http://localhost/cgi-bin/hello_get.py?first_name=ZARA&last_name=ALI
Copy after login

The result is still the above string of error prompts, so I looked at the prompt carefully, the general meaning is:

Server internal error

The server encountered an internal error or was unable to complete my request due to misconfiguration

Please contact the server administrator via email to inform them of this error and what you did before this error occurred. What operation

For more information, please check the server error log

Because of this, I firmly believe that it is Because I configured the server wrong, when I was at a loss, I suddenly thought that the first program test1 could run correctly even when I had not modified any configuration files. Could it be that this error had nothing to do with the configuration files, so I Try to remove the statement declaring the interpreter path in the first line of test1 and run it again. The same error appears in the browser


So I am very relieved, because this confirms my guess. It has nothing to do with the configuration of the server, because my script file has Error, so the server cannot respond to the request. If this is the case, then the error of hello_get.py is also the same. Then the problem is much simpler. Just check whether there are any errors in the code.

Here I want to reflect on one of my bad habits: when I try to run a routine on the website, I always press ctrl C and then ctrl V Copy and paste it into your own editor. Simple programs are fine, but complex programs with many code blocks have the consequences of this habit: incorrect indentation format, and then running errors. This problem exists in Python This is especially obvious, because Python has very strict requirements on indentation, so you must not copy and paste Python programs into your own programs. You must type code by code to ensure that the indentation is under your control.

When I say this, I understand that people have already seen the error in my hello_get.py file - I just didn’t type it myself. In fact, I have tried copying online before The C language code is put into VC++6.0. On the surface, the syntax seems to be completely correct, but there is always an error below, and the prompt is incomprehensible, so when I encounter this situation, I can only think of this reason. This also warns everyone that the code must be typed into your program letter by letter by yourself, otherwise there will be errors that are difficult to detect.

Finally, let’s talk about how to correctly configure wamp (Apache) under Windows to correctly execute CGI scripts. The steps are very simple:

Open httpd.conf

Change line 371

ScriptAlias /cgi-bin/ "D:/wamp/bin/apache/apache2.4.9/cgi-bin/"前面的#号去掉,就是解除注释,将后面引号里的D:/wamp/bin/apache/apache2.4.9/cgi-bin/改成自己的cgi-bin所在的路径
Copy after login

将387行引号里的内容也改成自己的cgi-bin所在的路径

保存之后重启服务器即可

【相关推荐】

1. 详解cgi向文本或者数据库写入数据实例代码

2. 分享在IIS上用CGI方式运行Python脚本的实例教程

3. 使用CGI模块建立简单web页面教程实例

4. 什么是CGI?详细介绍Python CGI编程

5. 详解XML与现代CGI应用程序的示例代码

6. FastCGI 进程意外退出造成500错误

The above is the detailed content of Share an example tutorial of PythonCGI programming. 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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months 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)

SVM examples in Python SVM examples in Python Jun 11, 2023 pm 08:42 PM

Support Vector Machine (SVM) in Python is a powerful supervised learning algorithm that can be used to solve classification and regression problems. SVM performs well when dealing with high-dimensional data and non-linear problems, and is widely used in data mining, image classification, text classification, bioinformatics and other fields. In this article, we will introduce an example of using SVM for classification in Python. We will use the SVM model from the scikit-learn library

VUE3 Getting Started Example: Making a Simple Video Player VUE3 Getting Started Example: Making a Simple Video Player Jun 15, 2023 pm 09:42 PM

As the new generation of front-end frameworks continues to emerge, VUE3 is loved as a fast, flexible, and easy-to-use front-end framework. Next, let's learn the basics of VUE3 and make a simple video player. 1. Install VUE3 First, we need to install VUE3 locally. Open the command line tool and execute the following command: npminstallvue@next Then, create a new HTML file and introduce VUE3: &lt;!doctypehtml&gt;

The relationship between the number of Oracle instances and database performance The relationship between the number of Oracle instances and database performance Mar 08, 2024 am 09:27 AM

The relationship between the number of Oracle instances and database performance Oracle database is one of the well-known relational database management systems in the industry and is widely used in enterprise-level data storage and management. In Oracle database, instance is a very important concept. Instance refers to the running environment of Oracle database in memory. Each instance has an independent memory structure and background process, which is used to process user requests and manage database operations. The number of instances has an important impact on the performance and stability of Oracle database.

Learn best practice examples of pointer conversion in Golang Learn best practice examples of pointer conversion in Golang Feb 24, 2024 pm 03:51 PM

Golang is a powerful and efficient programming language that can be used to develop various applications and services. In Golang, pointers are a very important concept, which can help us operate data more flexibly and efficiently. Pointer conversion refers to the process of pointer operations between different types. This article will use specific examples to learn the best practices of pointer conversion in Golang. 1. Basic concepts In Golang, each variable has an address, and the address is the location of the variable in memory.

VAE algorithm example in Python VAE algorithm example in Python Jun 11, 2023 pm 07:58 PM

VAE is a generative model, its full name is VariationalAutoencoder, which is translated into Chinese as variational autoencoder. It is an unsupervised learning algorithm that can be used to generate new data, such as images, audio, text, etc. Compared with ordinary autoencoders, VAEs are more flexible and powerful and can generate more complex and realistic data. Python is one of the most widely used programming languages ​​and one of the main tools for deep learning. In Python, there are many excellent machine learning and deep

Verification code usage examples in Gin framework Verification code usage examples in Gin framework Jun 23, 2023 am 08:10 AM

With the popularity of the Internet, verification codes have become a necessary process for login, registration, password retrieval and other operations. In the Gin framework, implementing the verification code function has become extremely simple. This article will introduce how to use a third-party library to implement the verification code function in the Gin framework, and provide sample code for readers' reference. 1. Install dependent libraries Before using the verification code, we need to install a third-party library goCaptcha. To install goCaptcha, you can use the goget command: $goget-ugithub

PHP simple web crawler development example PHP simple web crawler development example Jun 13, 2023 pm 06:54 PM

With the rapid development of the Internet, data has become one of the most important resources in today's information age. As a technology that automatically obtains and processes network data, web crawlers are attracting more and more attention and application. This article will introduce how to use PHP to develop a simple web crawler and realize the function of automatically obtaining network data. 1. Overview of Web Crawler Web crawler is a technology that automatically obtains and processes network resources. Its main working process is to simulate browser behavior, automatically access specified URL addresses and extract all information.

GAN algorithm example in Python GAN algorithm example in Python Jun 10, 2023 am 09:53 AM

Generative Adversarial Networks (GAN) is a deep learning algorithm that uses two neural networks to compete with each other to generate new data. GAN is widely used for generation tasks in image, audio, text and other fields. In this article, we will use Python to write an example of a GAN algorithm for generating images of handwritten digits. Dataset Preparation We will use the MNIST data set as our training data set. The MNIST data set contains

See all articles