1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| from fastapi import FastAPI
from fastapi import Response
import uvicorn
app = FastAPI()
@app.get("/images/{path}")
def get_pic(path: str): with open(f"source/images/{path}", "rb") as f: data = f.read() return Response(content=data, media_type="jpg") @app.get("/{path}") def get_html(path: str): with open(f"source/html/{path}", 'rb') as f: data = f.read() return Response(content=data, media_type="text/html")
uvicorn.run(app, host="127.0.0.1", port=8000)
|
深入学习:https://github.com/liaogx/fastapi-tutorial