FastApi

FastAPI 是一个基于Python3.6+版本用于构建API的高性能web框架

官方文档链接:https://fastapi.tiangolo.com

安装

执行pip install fastapi[all]来安装fastapi所需要的全部python包和模块

也可以分开来安装来将应用程序部署到成产环境,其中uvicorn可以用作运行代码的服务器

1
2
pip install fastapi
pip install uvicorn[standard]

接口

最简单的Fastapi接口可以如下所示,制作一个根节点下的欢迎目录Hello World

1
2
3
4
5
6
7
8
9
10
11
12
from fastapi import FastAPI

app = FastAPI()


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

if __name__ == "__main__":
import uvicorn
uvicorn.run(app="main:app", reload=True, host='127.0.0.1', port=8888)

进入对应的本地地址后会获得200的响应并收到json信息:{“message”: “Hello World”}


继续跳转到http://localhost:8888/docs或者http://localhost:8888/redoc将进入由Swagger UI提供的交互式API在线文档


Powered by Hexo and Hexo-theme-hiker

Copyright © 2017 - 2024 青域 All Rights Reserved.

UV : | PV :