如何在IIS上使用CGI部署Python应用程序?

Sat*_*mar 5 python iis cgi

我是 python 新手,我尝试在 IIS 上部署一个简单的 hello python 应用程序,我遵循了这个 URL

https://support.sisense.com/hc/en-us/community/posts/115007362727-Installing-Python-on-IIS

你好.py

print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print '</body>'
print '</html>'
Run Code Online (Sandbox Code Playgroud)

但是,它的错误如下所述

HTTP Error 401.3 - Unauthorized
You do not have permission to view this directory or page because of the access control list (ACL) configuration or encryption settings for this resource on the Web server
Run Code Online (Sandbox Code Playgroud)

我觉得这个问题可能与权限无关,因为我可以浏览 hello.html

我尝试了 SO 中提供的一些解决方案来解决该问题,但没有任何效果。非常感谢任何帮助

Jal*_*hal 5

要使用 IIS 配置 Python,您可以尝试按照以下步骤操作:

  1. 下载最新的 Python 版本,因为 IIS 无法使用旧版本的 python。

https://www.python.org/downloads/windows/

  1. 以下是hello.py文件:
print("Content-type:text/html\r\n\r\n")
print('<html>')
print('<head>')
print('<title>Hello Word - First CGI Program</title>')
print('</head>')
print('<body>')
print('<h2>Hello Word! This is my first CGI program</h2>')
print('</body>')
print('</html>')
Run Code Online (Sandbox Code Playgroud)
  1. 启用 IIS CGI 功能。

在此输入图像描述

  1. 打开 IIS 管理器。右键单击服务器名称并选择添加站点。

在此输入图像描述

  1. 添加站点绑定详细信息文件夹路径(python 文件夹)

在此输入图像描述

  1. 选择一个站点并单击中间窗格中的处理程序映射。

在此输入图像描述

  1. 单击操作窗格中的添加脚本映射。

在此输入图像描述

  1. 添加脚本映射值。

*.py,并将其映射到c:\Python37-32\python.exe %s %s.

在此输入图像描述

在此输入图像描述

确保目录浏览已启用。

在此输入图像描述

web.config文件:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="python" path="*.py" verb="*" modules="CgiModule" scriptProcessor="C:\Python37-32\python.exe %s %s" resourceType="File" />
        </handlers>
        <directoryBrowse enabled="true" />
    </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

设置站点文件夹( ) 和python 文件夹( ) 的iis_iusrs权限。确保启用匿名身份验证,并将应用程序池设置为应用程序池标识。完成所有更改后,重新启动 IIS 服务器并浏览站点。iusrc:\pythonappC:\Python37-32

在此输入图像描述