Python Fastapi:1.Fastapi介绍;GET和POST请求的写法

该文章已生成可运行项目,

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}
本文章已经生成可运行项目
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值