Share an example tutorial of PythonCGI programming
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/
and thenModify the httpd.conf file as follows:
<Directory "/var/www/cgi-bin"> AllowOverride None Options +ExecCGI Order allow,deny Allow from all</Directory>
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 '<html>'print '<head>'print '<title>Hello Word - First CGI Program</title>'print '</head>'print '<body>'print '<h2>Hello Word! This is my first CGI program</h2>'print '</body>'print '</html>'
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
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('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>Hello %s %s</h2>" % (first_name, last_name)print "</body>"print "</html>"
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
editor Then enter again in the browser:
http://localhost/cgi-bin/hello_get.py?first_name=ZARA&last_name=ALI
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所在的路径
将387行
保存之后重启服务器即可
【相关推荐】
2. 分享在IIS上用CGI方式运行Python脚本的实例教程
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

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

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: <!doctypehtml>

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.

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 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

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

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.

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
