Table of Contents
Hello World!
Home Backend Development Python Tutorial 在IIS服务器上以CGI方式运行Python脚本的教程

在IIS服务器上以CGI方式运行Python脚本的教程

Jun 10, 2016 pm 03:14 PM
python

由于接触到Python Web开发,正好把最简单的CGI方式研究了一下,话说在Windows下配置Python的Web开发还真的蛮麻烦的,Linux下配置倒挺容易,正好微软有技术文章《Using Python Scripts with IIS》介绍了这些内容,此文介绍了两种方法,一是使用ASP引擎来运行Python脚本,这个可能需要用到ActivePython,当然ASP技术已经过时了,我今天就简单介绍下CGI模块运行方式。

编写简单的支持CGI的Python脚本(本文介绍3.2版本的Python):

print("Status: 200 OK")
print("Content-type: text/html")
print() # 打印一行空白行,用于分隔HTTP Header和正文
 
print("<h1 id="Hello-World">Hello World!</h1>")

Copy after login

这样就可以了,大家可以猜出CGI是将标准输出流重新定向到HTTP输出流来实现网页或者数据传输的。

当然这个在IIS中是不能直接运行的,我们需要配置一下,打开Internet 信息服务(IIS)管理器界面,选择“处理程序映射”。

2015425161112268.png (373×139)

在接下来出现的界面右侧选择“添加模块映射”。

IIS7添加模块映射

2015425161152223.png (572×250)

假设我们的Python 3.2安装于C:\Python32,那么可以向下图这样填写:

2015425161216044.png (397×318)

然后点击确定,在接下来出现的对话框选择“是”。

2015425161237176.png (416×161)

好了,我们的配置完成了,重启一下IIS,然后赶快试试刚才的代码吧。可能有人会抱怨,用CGI编写网页一旦报错调试会比较麻烦,比如报下面的错误:

HTTP 错误 502.2 - Bad Gateway

指定的 CGI 应用程序由于未返回完整的一组 HTTP 头而产生错误行为。它实际返回的头是“Traceback (most recent call last): File "E:\projects\test.py", line 3, in 1/0 ZeroDivisionError: division by zero ”。

其实我们只需要在最开始引入import cgitb; cgitb.enable()就可以了,就像下面这样:

import cgitb; cgitb.enable()
print("Status: 200 OK")
print("Content-type: text/html")
print() # 打印一行空白行,用于分隔HTTP Header和正文
 
print("<h1 id="Hello-World">Hello World!</h1>")

Copy after login

这样一旦出错,就会以友好的方式将错误输出来。

2015425161301420.png (610×391)

对于表单的处理,可以参考import cgi模块(cgi.FieldStorage),网上有很多此方面的介绍,我就不多说了,Enjoy it!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can the Python interpreter be deleted in Linux system? Can the Python interpreter be deleted in Linux system? Apr 02, 2025 am 07:00 AM

Regarding the problem of removing the Python interpreter that comes with Linux systems, many Linux distributions will preinstall the Python interpreter when installed, and it does not use the package manager...

How to solve the problem of Pylance type detection of custom decorators in Python? How to solve the problem of Pylance type detection of custom decorators in Python? Apr 02, 2025 am 06:42 AM

Pylance type detection problem solution when using custom decorator In Python programming, decorator is a powerful tool that can be used to add rows...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Apr 02, 2025 am 06:27 AM

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Do FastAPI and aiohttp share the same global event loop? Do FastAPI and aiohttp share the same global event loop? Apr 02, 2025 am 06:12 AM

Compatibility issues between Python asynchronous libraries In Python, asynchronous programming has become the process of high concurrency and I/O...

What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6? What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6? Apr 02, 2025 am 07:12 AM

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

How to ensure that the child process also terminates after killing the parent process via signal in Python? How to ensure that the child process also terminates after killing the parent process via signal in Python? Apr 02, 2025 am 06:39 AM

The problem and solution of the child process continuing to run when using signals to kill the parent process. In Python programming, after killing the parent process through signals, the child process still...

See all articles