简介
Fastapi是一款现代的、高性能的web框架, 用于基于标准Python类型提示使用Python 3.6+构建API.
主要特点有:
高性能,与NodeJS和Go相当,最快的python 框架之一
快速编写代码:将功能开发的速度提高大约200%至300%
更少的错误:减少约40%的人为错误(开发人员)
直观:强大的编辑器支持,花费调试时间更少
简易:旨在易于使用和学习, 减少阅读文档的时间。
简短:减少代码重复
稳健:获取可用于生产环境的代码, 具有自动交互式文档。
标准化: 基于(并完全兼容)API的开放标准:OpenAPI(以前称为Swagger)和JSON模式。
安装
#pip install fastapi uvicorn
进入项目目录,运行
#uvicorn main:app --reload...
代码实例
# -*- coding: utf-8 -*-
# @Time : 2020/7/3 2:16 上午
# @Author : renwoxing
# @File : main.py
# @Software: PyCharm
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
is_offer: bool
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/item/{item_id}")
def get_item(item_id: int, q: str=None):
return {"item": item_id, "q": q}
@app.put("/item/{item_id}")
def update_item(item_id: int, item: Item):
return {"item_id": item_id, "item_name": item.name}
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='127.0.0.1', port=8080)
交互式文档
http://127.0.0.1:8080/docs#/
API文档
http://127.0.0.1:8080/redoc
本文地址:https://blog.csdn.net/qq_21127151/article/details/107140786
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!