在Windows 10 IIS服务器上托管Python Flask

2024-05-14 17:36:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在Windows 10 IIS服务器上托管Python Rest API

首先,我试图托管一个示例应用程序,但无法实现这一点

my_app.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello IIS from Flask framework.'

@app.route('/Hello')
def hello_world():
    return 'Hello World!'
    
if __name__ == '__main__':
    app.run()

web.config

<configuration>  
  <system.webServer>
    <handlers>
        <add name="Python FastCGI"
            path="*"
            verb="*"
            modules="FastCgiModule"
            scriptProcessor="C:\Program Files\Python39\python.exe|C:\Program Files\Python39\lib\site-packages\wfastcgi.py"
            resourceType="Unspecified"
            requireAccess="Script" />
    </handlers>
  </system.webServer>
  <appSettings>
    <add key="WSGI_HANDLER" value="my_app.app" /> <!-- {name_of_file}.{name_of_flask_app}-->
    <add key="PYTHONPATH" value="C:\inetpub\wwwroot\test" />
    <add key="WSGI_LOG" value="C:\inetpub\wwwroot\test\app.log" />
  </appSettings>
</configuration>  

我试过下面的教程,但对我不起作用

  1. https://medium.com/@rajesh.r6r/deploying-a-python-flask-rest-api-on-iis-d8d9ebf886e9

  2. https://www.youtube.com/watch?v=ma1UvzqF82Q&ab_channel=ShobhitWalia

HTTP错误500.0-内部服务器错误:see the Error scrennshot

我需要帮助。。。。谢谢


Tags: keynamefrompy服务器addappflask
1条回答
网友
1楼 · 发布于 2024-05-14 17:36:47

如果您想要IIS宿主python应用程序,则需要安装python,然后需要在IIS中添加模块映射。以下是步骤:

  1. 首先在您的计算机上安装python

  2. 启用CGI:

enter image description here

成功安装后,您将看到“ISAPI和CGI限制”和处理程序映射:

enter image description here

  1. 输入“ISAPI和CGI限制”,添加新的ISAPI或CGI限制:

enter image description here

  1. 输入“处理程序映射”,然后添加模块映射(%S%S需要添加,否则将报告错误):

enter image description here

最后,我们可以成功访问python应用程序:

enter image description here

更新:

您能在处理程序映射中看到python吗

enter image description here

这是我的web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
</configuration>

web.config中没有任何内容。我的所有设置都适用于整个IIS:

enter image description here

点击“目录浏览”:

enter image description here

启用它:

enter image description here

相关问题 更多 >

    热门问题