The tutorial of running Python scripts in CGI Mode on the IIS server, iispython
Due to the exposure to Python Web development, I just studied the simplest CGI method. It is really troublesome to configure Python Web development in Windows. It is quite easy to configure Python in Linux, microsoft's technical article Using Python Scripts with IIS introduces this content. This article introduces two methods. One is to use the ASP engine to run Python Scripts, which may require ActivePython, of course ASP technology is outdated. Today I will briefly introduce the CGI module running mode.
Compile a simple Python script that supports CGI (this article introduces Python 3.2 ):
Print ("Status: 200 OK") print ("Content-type: text/html") print () # print a blank line, used to separate the HTTP Header and body print ("
In this way, we can guess that CGI redirects the standard output stream to the HTTP output stream to implement webpage or data transmission.
Of course, this cannot be run directly in IIS. We need to configure it to open the Internet Information Service (IIS) Manager interface and select "handler ing ".
Select "add module ing" on the right of the page that appears ".
IIS7 add module ing
Assume that Python 3.2 is installed in C: \ Python32:
Click "OK" and select "yes" in the following dialog box ".
Okay, our configuration is complete. Restart IIS and try the code just now. Some people may complain that it may be difficult to debug a webpage by Using CGI. For example, the following error is reported:
HTTP: 502.2-Bad Gateway
The specified CGI application produces an error because it does not return a complete set of HTTP headers. The actual returned header is "Traceback (most recent call last): File" E: \ projects \ test. py ", line 3, in <module> 1/0 ZeroDivisionError: division by zero ".
In fact, we only need to introduce import cgitb; cgitb. enable () at the very beginning, as shown below:
Import cgitb; cgitb. enable () print ("Status: 200 OK") print ("Content-type: text/html") print () # print a blank line, used to separate the HTTP Header and body print ("
In this way, once an error occurs, the error is output in a friendly way.
For form processing, refer to the import cgi Module (cgi. FieldStorage). There are many introductions on the Internet, so I will not talk about it much. Enjoy it!