一、安装FastAPI
FastAPI - 是一个现代的,快速(高性能)python web框架,用于基于标准Python类型提示使用Python 3.6+构建API(https://fastapi.tiangolo.com/)。
FastAPI基于以下
使用Uvicorn服务器ASGI规范(ASGI是异步服务器网关接口 是WSGI的精神继承者,在具有异步功能的Python Web服务器,框架和应用程序之间提供标准接口。)
Starlette:是一种轻量级的ASGI框架/工具包,是构建高性能异步服务的理想选择(https://www.starlette.io/)。
Pydantic:用于验证数据(https://pydantic-docs.helpmanual.io/)。
uvicorn:主要用于加载和提供应用程序的服务器(http://www.uvicorn.org/)。
1、通过以下命令,安装所有安装包:
# pip3 install fastapi[all]
2、安装成功后,显示:

3、查看已经安装好的库:

二、第一个 Hello World
from fastapi import FastAPI
import uvicorn
app = FastAPI()
@app.get("/")
async def root():
return {"message":"Hello world, FastAPI"}
# Press the green button in the gutter to run the script.
if __name__ =='__main__':
uvicorn.run(app='main:app', host="127.0.0.1", port=5555, reload=True, debug=True)
三、访问首页
http://127.0.0.1:5555/
四、交互API文档
http://127.0.0.1:5555/docs
你将会看到自动生成的API交互文档。
五、替代API文档
http://127.0.0.1:5555/redoc
网友评论