官方文档:https://fastapi.tiangolo.com/zh/advanced/wsgi/
包含 WSGI - Flask,Django,其它¶
您可以挂载多个 WSGI 应用,正如您在 Sub Applications - Mounts, Behind a Proxy 中所看到的那样。
为此, 您可以使用 WSGIMiddleware
来包装你的 WSGI 应用,如:Flask,Django,等等。
使用 WSGIMiddleware
¶
您需要导入 WSGIMiddleware
。
然后使用该中间件包装 WSGI 应用(例如 Flask)。
之后将其挂载到某一个路径下。
from fastapi import FastAPI from fastapi.middleware.wsgi import WSGIMiddleware from flask import Flask, request from markupsafe import escape 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" }
实践
使用中间件来挂载pyramid web应用
示例1
源代码
from fastapi import FastAPI
from fastapi.middleware.wsgi import WSGIMiddleware
from pyramid.config import Configurator
from pyramid.response import Responsedef pyramid_app(environ, start_response):config = Configurator()def hello_world(request):return Response('Hello from Pyramid!')config.add_route('hello', '/')config.add_view(hello_world, route_name='hello')app = config.make_wsgi_app()return app(environ, start_response)app = FastAPI()
app.mount("/pyramid", WSGIMiddleware(pyramid_app))@app.get("/")
async def read_main():return {"message": "Hello from FastAPI"}if __name__ == "__main__":import uvicornuvicorn.run(app, host="0.0.0.0", port=8000)
启动服务
python">uvicorn fastapipyramid:app --reload
测试
python">curl 127.0.0.1:8000
{"message":"Hello from FastAPI"}
python">curl 127.0.0.1:8000/pyramid/
Hello from Pyramid!
示例2
源代码
python">from fastapi import FastAPI, Request
from fastapi.middleware.wsgi import WSGIMiddleware
from pyramid_app import create_app # 导入 Pyramid 应用# 创建 FastAPI 应用
app = FastAPI()# 创建 Pyramid WSGI 应用
pyramid_app = create_app()# 使用 WSGIMiddleware 将 Pyramid 应用包装到 FastAPI 中
app.mount("/pyramid", WSGIMiddleware(pyramid_app))# 你也可以在 FastAPI 中定义其他路由
@app.get("/fastapi")
def read_root():return {"message": "Hello from FastAPI!"}
启动服务:
python">uvicorn pyramid_fastapi:app --reload
测试
python">curl 127.0.0.1:8000/fastapi
{"message":"Hello from FastAPI!"}
python">curl 127.0.0.1:8000/pyramid/
Hello from Pyramid!
调试
启动pyramid报错
from backports import tarfile
ImportError: cannot import name 'tarfile' from 'backports' (/Users/skywalk/py311/lib/python3.11/site-packages/backports/__init__.py)
python pyramid_app.py
Traceback (most recent call last):File "/Users/skywalk/work/fastapi/pyramid_app.py", line 3, in <module>from pyramid.config import ConfiguratorFile "/Users/skywalk/py311/lib/python3.11/site-packages/pyramid/config/__init__.py", line 8, in <module>from pyramid.asset import resolve_asset_specFile "/Users/skywalk/py311/lib/python3.11/site-packages/pyramid/asset.py", line 2, in <module>import pkg_resourcesFile "/Users/skywalk/py311/lib/python3.11/site-packages/pkg_resources/__init__.py", line 96, in <module>from jaraco.text import (File "/Users/skywalk/py311/lib/python3.11/site-packages/setuptools/_vendor/jaraco/text/__init__.py", line 12, in <module>from jaraco.context import ExceptionTrapFile "/Users/skywalk/py311/lib/python3.11/site-packages/setuptools/_vendor/jaraco/context.py", line 17, in <module>from backports import tarfile
ImportError: cannot import name 'tarfile' from 'backports' (/Users/skywalk/py311/lib/python3.11/site-packages/backports/__init__.py)
pip安装包解决
python">pip install jaraco.context