Windows 10 IIS服务器上的Python Flask托管 [英] Python Flask Hosting on Windows 10 IIS server
问题描述
我想在Windows 10 IIS服务器上托管我的Python Rest API.
首先,我尝试托管一个示例应用程序,但无法做到这一点.
my_app.py
从烧瓶导入烧瓶的 app = Flask(__ name__)@ app.route('/')def index():返回来自Flask框架的Hello IIS".@ app.route('/你好')def hello_world():返回"Hello World!"如果__name__ =='__main__':app.run()
web.config
< configuration>< system.webServer><处理程序>< add name ="Python FastCGI";path ="*"动词="*"modules ="FastCgiModule"scriptProcessor ="C:\ Program Files \ Python39 \ python.exe | C:\ Program Files \ Python39 \ lib \ site-packages \ wfastcgi.py"resourceType =未指定"requireAccess =脚本"/></handlers></system.webServer>< appSettings><添加密钥="WSGI_HANDLER";值="my_app.app"/><!-{name_of_file}.{name_of_flask_app}->< add key ="PYTHONPATH";value =" C:\ inetpub \ wwwroot \ test"/>< add key ="WSGI_LOG"值="C:\ inetpub \ wwwroot \ test \ app.log"/></appSettings></configuration>
我尝试了以下教程,但对我来说不起作用.
-
成功安装后,您将看到"ISAPI和CGI限制"和处理程序映射:
- 输入"ISAPI和CGI限制",添加新的ISAPI或CGI限制:
- 输入处理程序映射",然后添加模块映射(需要添加%S%S或将报告错误):
最后,我们可以成功访问python应用程序:
更新:
您可以在处理程序映射中看到python吗?
这是我的web.config:
<?xml version ="1.0"encoding ="UTF-8"?<配置></configuration>
web.config中没有任何内容.我所有的设置都是针对整个IIS的:
点击目录浏览":
启用:
I Want to Host my Python Rest API on Windows 10 IIS server.
First I tried to host a sample application but can not able to that.
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>
I have tried the below tutorials but it is not working for me.
https://medium.com/@rajesh.r6r/deploying-a-python-flask-rest-api-on-iis-d8d9ebf886e9
https://www.youtube.com/watch?v=ma1UvzqF82Q&ab_channel=ShobhitWalia
HTTP Error 500.0 - Internal Server Error : see the Error scrennshot
I need help.... Thanks
解决方案If you want IIS host python application, you need to install python, and then you need to add module mapping in IIS. Here are the steps:
First install python on your computer.
Enable CGI:
After successful installation, you will see "ISAPI and CGI Restrictions" and Handler Mappings:
- Enter "ISAPI and CGI Restrictions", add a new ISAPI or CGI Restrictions:
- Enter "Handler Mappings", then add Module Mapping(%S %S needs to be added or an error will be reported):
Finally we can successfully access the python Application:
UPDATE:
Can you see python in your Handler Mappings?
This is my web.config:
<?xml version="1.0" encoding="UTF-8"?> <configuration> </configuration>
There is nothing in web.config. All my settings are for the entire IIS:
Click "Directory Browsing":
Enable it:
这篇关于Windows 10 IIS服务器上的Python Flask托管的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!