In the last week, I continued to learn Python online, and I was exposed to the CGI programming of Python, which is officially defined as follows:
CGI (Common Gateway Interface), a universal Gateway interface, is a program that runs on a server such as an HTTP server, providing an interface with the client HTML page.
I am more interested in the Python CGI can write a lot of applications, so I just started to learn the Python programming part, first to configure the Web server support CGI, I have installed the WAMP integrated development environment on my computer, so I do not have to install Apache, There are a lot of blog posts on the Internet is how to configure under Apache, and it is in the Linux environment, but I want to configure the file content should not change very much, so I began my journey of tinkering. It turns out that using Windows to do development is really a pit.
According to the online learning website Python CGI, to set up a good CGI directory, in the Apache configuration file modified httpd.conf
scriptalias/cgi-bin//var/www/cgi-bin/
again Modify 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 the AddHandler so that we can access the Python script file at the end of the. PY:
After all this, restart the server (apache/wamp) and write the first CGI program with the following code:
test1.py
#! D:\Python27\python #-*-Coding:utf-8-*-print "content-type:text/html\r\n\r\n" print '
The contents of the first line specify the location of the script interpreter, which can be changed according to the path you have installed
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
There's no problem.
Then write a second program:hello_get.py
#! d:\python27\python#-*-coding:utf-8-*-# CGI processing module import CGI, CGITB # creates an instantiated form of fieldstorage = CGI. Fieldstorage () # get Data first_name = Form.getvalue (' first_name ') last_name = Form.getvalue (' last_name ') print " content-type:text/html\r\n\r\n "print"
In the browser input Localhost/cgi-bin/hello_get.py?first_name=zara&last_name=ali
But it's not as output as it says on the website.
Hello ZARA ALI
Instead, the following error message appears
So I wonder, configuration environment, code is exactly the same, where is the problem, and then I Baidu Windows Apache CGI configuration method, but out of the Linux configuration method, so no way, I chose to reinstall Wamp and then reconfigure the CGI environment, Then I have done each of the above steps in turn, and then copied the code from the site to the editor and entered it again in the browser:
Http://localhost/cgi-bin/hello_get.py?first_name=ZARA&last_name=ALI
The result is still a bunch of errors above, so I looked at the hint carefully, which is basically saying:
Server Internal Error
An internal error occurred on the server or failed to complete my request due to misconfiguration
Please email the server administrator to tell the error and what you did before the error occurred.
For more information, please check the server error log
Because of this I firmly believe that because I put the server misconfiguration, just when I helpless, I suddenly thought, I did not modify any configuration file, the first program test1 will be able to run correctly, will this error is not related to the configuration file, So I tried to get rid of the one statement that test1 the first line of the Declaration interpreter path, ran again, and the same error occurred in the browser.
So I am very gratified, because this confirms my conjecture, and the server configuration is not related, because my script file is wrong, so the server can not respond to the request, in this case, the hello_get.py error is the same, then the problem is much simpler, as long as the code is not written in the wrong place.
Here I would like to reflect on a bad habit: when I try to run a routine on the site, I always a CTRL c and then CTRL v copy paste into their own editor, a simple program is OK, more complex, code block a lot of programs, this habit brings the result is: Indentation format is not correct, Then run the error, which is particularly noticeable in Python, because Python is really strict with indentation requirements, so you can't copy and paste the Python program into your own program, be sure to code a code to knock, to ensure that indentation under their own control.
I said here that the wise have seen my hello_get.py file of the wrong place-just do not knock yourself, in fact, I have tried in the online copy of the C language code into the vc++6.0 inside, the surface looks like the syntax is completely correct, but there is always a mistake, Hints are also not understood, so in this case I can think of this may be the reason. This also warns that the code must do its own hand a letter to a symbol to knock into your program, otherwise there will be difficult to detect errors.
Finally, the steps to properly configure Wamp (Apache) under Windows to properly execute CGI scripts are simple:
Open httpd.conf
The 371-row
scriptalias/cgi-bin/"D:/wamp/bin/apache/apache2.4.9/cgi-bin/" before the # is removed, is to dismiss the comment, the quotation marks in the back of the d:/wamp/bin/apache/ Apache2.4.9/cgi-bin/changed to the path of his own cgi-bin.
Change the contents of the 387 line <directory "D:/wamp/bin/apache/apache2.4.9/cgi-bin" > quotes to the same path as your Cgi-bin
Restart the server after saving
"Recommended"
1. Detailed CGI writing data instance code to text or database
2. Sharing an example tutorial on using CGI to run Python scripts on IIS
3. Creating a simple Web page tutorial instance using the CGI module
4. What is CGI? Detailed description of Python CGI programming
5. Sample code for XML and modern CGI applications
6. The FastCGI process unexpectedly exits resulting in a 500 error