我正在尝试在 IIS 上托管简单的 FastAPI 应用程序。
app.py
from fastapi.middleware.wsgi import WSGIMiddleware
from flask import Flask, escape, request
flask_app = Flask(__name__)
@flask_app.route("/")
def flask_main():
name = request.args.get("name", "World")
return f"Hello, {escape(name)} from Flask!"
app = FastAPI()
@app.get("/v2")
def read_main():
return {"message": "Hello World"}
app.mount("/v1", WSGIMiddleware(flask_app))
web.config
<configuration>
<system.webServer>
<handlers>
<add name="Python FastCGI" path="*" verb="*" modules="FastCgiModule"
scriptProcessor="C:\Users\oniel\AppData\Local\Programs\Python\Python39\python.exe|C:\Users\oniel\AppData\Local\Programs\Python\Python39\Lib\site-packages\wfastcgi.py"
resourceType="Unspecified" requireAccess="Script" />
</handlers>
</system.webServer>
<appSettings>
<add key="WSGI_HANDLER" value="my_app.app" />
<add key="PYTHONPATH" value="C:\inetpub\wwwroot\Flask_Demo" />
<!-- <add key="WSGI_LOG" value="C:\inetpub\wwwroot\Flask_Demo\app.log" />-->
</appSettings>
</configuration>
将 WSGI_HANDLER 更改为 my_app.flask_app,然后就可以了。
为 fastAPI 给出的错误:
Error occurred:
Traceback (most recent call last):
File "c:\users\oniel\appdata\local\programs\python\python39\lib\site-packages\wfastcgi.py", line 847, in main
result = handler(record.params, response.start)
TypeError: __call__() missing 1 required positional argument: 'send'
StdOut:
StdErr: