Python用fastapi来建立异步的web服务器
from fastapi import FastAPI
import uvicorn
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello"}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8080)
执行服务器程序
D:\code\python\http>python FastAPI1.py
[32mINFO[0m: Started server process [[36m559768[0m]
[32mINFO[0m: Waiting for application startup.
[32mINFO[0m: Application startup complete.
[32mINFO[0m: Uvicorn running on [1mhttp://0.0.0.0:8080[0m (Press CTRL+C to quit)
运行结果
C:\Users\zjf>curl http://127.0.0.1:8080
{"message":"Hello"}