The tutorial of running Python scripts in CGI Mode on the IIS server, iispython

Source: Internet
Author: User

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!

Related Article

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.