最小 FastAPI webserver 示例

这是最小 FastAPI webserver

minimal_fastapi_app.py
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def read_root():
    return {"message": "Hello, World!"}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="127.0.0.1", port=8000)

运行此脚本,然后在浏览器中访问 http://localhost:8000 查看:

example_response.json
{"message": "Hello, World!"}

Check out similar posts by category: FastAPI, Python