WSGI 포함하기 - Flask, Django 등¶
🌐 Translation by AI and humans
This translation was made by AI guided by humans. 🤝
It could have mistakes of misunderstanding the original meaning, or looking unnatural, etc. 🤖
You can improve this translation by helping us guide the AI LLM better.
서브 애플리케이션 - 마운트, 프록시 뒤에서에서 본 것처럼 WSGI 애플리케이션을 마운트할 수 있습니다.
이를 위해 WSGIMiddleware를 사용해 WSGI 애플리케이션(예: Flask, Django 등)을 감쌀 수 있습니다.
WSGIMiddleware 사용하기¶
정보
이를 사용하려면 a2wsgi를 설치해야 합니다. 예: pip install a2wsgi
a2wsgi에서 WSGIMiddleware를 import 해야 합니다.
그런 다음, WSGI(예: Flask) 애플리케이션을 미들웨어로 감쌉니다.
그리고 해당 경로에 마운트합니다.
from a2wsgi import WSGIMiddleware
from fastapi import FastAPI
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))
참고
이전에 fastapi.middleware.wsgi의 WSGIMiddleware 사용을 권장했지만 이제는 더 이상 권장되지 않습니다.
대신 a2wsgi 패키지 사용을 권장합니다. 사용 방법은 동일합니다.
단, a2wsgi 패키지가 설치되어 있고 a2wsgi에서 WSGIMiddleware를 올바르게 import 하는지만 확인하세요.
확인하기¶
이제 /v1/ 경로에 있는 모든 요청은 Flask 애플리케이션에서 처리됩니다.
그리고 나머지는 FastAPI에 의해 처리됩니다.
실행하고 http://localhost:8000/v1/로 이동하면 Flask의 응답을 볼 수 있습니다:
Hello, World from Flask!
그리고 http://localhost:8000/v2로 이동하면 FastAPI의 응답을 볼 수 있습니다:
{
"message": "Hello World"
}