Because of the access to Python web development, just the simplest CGI way to study, saying that in Windows to configure Python Web development is really cumbersome, Linux configuration is quite easy, just Microsoft has technical article "Using Python Scripts With IIS, this article describes the two methods, one is to use the ASP engine to run the Python script, this may need to use ActivePython, of course, ASP technology is outdated, I will briefly introduce the next CGI module operation mode.
Write a simple Python script that supports CGI (this article describes the 3.2 version of Python):
The print ("status:200 OK") print (
"content-type:text/html") print (
) # prints a blank line that separates the HTTP header and body
print ("
This is OK, you can guess that the CGI is to redirect the standard output stream to the HTTP output stream to implement the Web page or data transmission.
Of course this is not directly running in IIS, we need to configure, open the Internet Information Services (IIS) Manager interface, select "Handler mapping."
Select Add Module mapping on the right side of the interface that appears next.
IIS7 Add a module map
Assuming that our Python 3.2 is installed in C:\Python32, you can fill it down as follows:
Then click OK and select "Yes" in the dialog box that appears next.
OK, our configuration is complete, restart IIS, and try the code just now. Some people may complain, write a Web page with CGI, once the error debugging will be more trouble, such as the report of the following errors:
HTTP Error 502.2-bad Gateway
The specified CGI application generated an error behavior because it did not return a complete set of HTTP headers. It actually returns the head is "traceback (most recent call last): File" E:\projects\test.py ", line 3, in <module> 1/0 zerodivisionerror:d Ivision by Zero ".
In fact, we only need to introduce import CGITB at the very beginning; Cgitb.enable () is OK, just like the following:
Import CGITB; Cgitb.enable () print (
"status:200 OK") print (
"content-type:text/html") print (
) # prints a blank line that separates the HTTP Header and Body
print ("
This way, if you make an error, you will output the error in a friendly way.
For processing of forms, you can refer to the Import CGI module (CGI). Fieldstorage), the internet has a lot of this introduction, I will not say more, Enjoy it!