Tutorial on running Python scripts in CGI mode on IIS server

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.