0%

fastapi | 简单的介绍

简单的介绍一下 fastapi

fastapi 内部封装了

  • Starlette
  • Pydantic

搭建 fastapi 项目

1
2
pip install fastapi
pip install "uvicorn[standard]" # 这个是安装服务端

fastapi 可能和 web3 这个库有冲突,因为,这两个需要的 websocket 的版本不一致,所以,要记得这个点。

开启服务

使用 main.py 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from typing import Optional

from fastapi import FastAPI

app = FastAPI()


@app.get('/')
def hello_world():
return {'hello': 'world'}


@app.get('/city/{city}')
def result(city: str, query_string: Optional[str] = None):
return {'city': city, 'query_string': query_string}

然后开启服务

1
uvicorn main:app --reload

如果访问

1
2
http://127.0.0.1:8000/
http://127.0.0.1:8000/city/city?query_string=23

就会获得设定好的返回值。

请我喝杯咖啡吧~