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!