Tutorial on running Python scripts as CGI on an IIS server _python

Source: Internet
Author: User
Tags python script

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!

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.