Fast API
异步框架,支持高并发,方法前面可以配合async进行异步通信
需要安装两个东西:fastAPI、uvicorn
同步:一件事情没有做完----卡住
异步:不会卡住,会继续进行
启动第一个程序:
from fastapi import FastAPI
import uvicorn
app = FastAPI()
@app.get("/")
async def hello():
return {"data": "HELLO!"}
if __name__ == "__main__":
uvicorn.run(app="main:app", host="0.0.0.0", port=8080, reload=False)
GET请求
使用fastAPI来接受GET请求的方法
FastAPI具有两种接收参数的方法
- 直接将参数写在路由里面。
@app.get("/get/{uid}")
async def read_get(uid: str):
return {"Now your uid": uid}
访问/get/1
得到Now your uid : 1
- 通过问号来输入参数
# 指明请求参数,即用?&
@app.get("/get2")
async def read_get2(uid: str, name: str):
return "Welcome!"+name+", your uid is"+uid
访问/get2?uid=3&name=xiaoming
“Welcome!xiaoming, your uid is3”
POST请求
需要安装python-multipart
from fastapi import FastAPI, Form
from typing import Optional
@app.post("/post1")
async def read_post1(uid:Optional[str] = Form()):
return {"uid": uid}
1666

被折叠的 条评论
为什么被折叠?



