This article mainly introduces how to run Python scripts in CGI mode on the IIS server, although the IIS performance is not satisfactory... for more information about how to configure Python Web development in Windows, configuration in Linux is quite easy. Microsoft has a technical article "Using Python Scripts with IIS", which introduces these content. This article introduces two methods, first, use the ASP engine to run Python scripts. This may require ActivePython. of course, ASP technology is outdated. I will briefly introduce the CGI module running mode today.
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 ("Hello World! ")
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 1/0 ZeroDivisionError: pision 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 ("Hello World! ")
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!