您可以像使用 Sub Applications-代理背后的Mounts 一样挂载WSGI应用程序 。
为此,您可以使用
WSGIMiddleware
和使用它来包装WSGI应用程序,例如Flask,Django等。
WSGIMiddleware
您需要导入
WSGIMiddleware
。
然后用中间件包装WSGI(例如Flask)应用程序。
然后将其安装在路径下。
from flask import Flask, escape, request
from fastapi import FastAPI
from fastapi.middleware.wsgi import WSGIMiddleware
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))
现在,
/v1/
Flask应用程序将处理
路径下的每个请求
。
其余的将由 FastAPI 处理 。
如果使用Uvicorn运行它并转到 http:// localhost:8000 / v1 /, 您将看到Flask的响应:
Hello, World from Flask!
如果您转到 http:// localhost:8000 / v2 ,则会看到来自FastAPI的响应:
{
"message": "Hello World"
}
文章来源互联网,如有侵权,请联系管理员删除。邮箱:417803890@qq.com / QQ:417803890
Python Free
邮箱:417803890@qq.com
QQ:417803890
皖ICP备19001818号-4
© 2019 copyright www.pythonf.cn - All rights reserved
微信扫一扫关注公众号:
Python Free