Python Fastapi:1.Fastapi介绍;GET和POST请求的写法
FastAPI是一个高性能异步Web框架,支持高并发处理。通过uvicorn服务器运行,开发者可以使用async/await语法实现异步通信。框架提供GET和POST请求处理:GET请求支持路径参数和查询参数两种方式;POST请求需安装python-multipart,通过Form接收表单数据。示例演示了基础路由配置、参数接收和异步响应返回,体现了FastAPI简洁高效的开发特点。
·
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}
更多推荐




所有评论(0)