跳转到主要内容

python restful API服务器fastAPI

组件

  • fastAPI : https://fastapi.tiangolo.com/
  • uvicorn: https://www.uvicorn.org/

快速开始

# 安装fastAPI
pip install fastapi -i https://pypi.tuna.tsinghua.edu.cn/simple

# 安装web容器
pip install "uvicorn[standard]" -i https://pypi.tuna.tsinghua.edu.cn/simple

# 安装from参数表单
pip install python-multipart -i https://pypi.tuna.tsinghua.edu.cn/simple

编写代码

# main.py

from fastapi import FastAPI

app = FastAPI()


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




# 运行
uvicorn main:app --reload
    
# 也可以用代码实现
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

# main:app <module:attribe>