摘要: HttpBasic基本认证 from fastapi import FastAPI, Depends from fastapi.security import HTTPBasic, HTTPBasicCredentials from fastapi.exceptions import HTTPExc 阅读全文
posted @ 2024-02-28 19:09 我在路上回头看 阅读(255) 评论(0) 推荐(0)
摘要: aioredis official website Install pip install aioredis Connect to redis from fastapi import FastAPI import aioredis app = FastAPI() @app.on_event('sta 阅读全文
posted @ 2024-02-28 19:04 我在路上回头看 阅读(1760) 评论(0) 推荐(0)
摘要: jwt认证 1.头部Header,主要是对jwt元数据的描述 { 'alg': 'HS256', 'typ': 'JWT' } 2.载荷playload,主要包含jwt信息需要传递的主体数据 { 'iss': 'jack', # 由jwt签发 'sub': 'jack', # 该jwt面向的用户组, 阅读全文
posted @ 2024-02-28 19:00 我在路上回头看 阅读(542) 评论(0) 推荐(0)
摘要: from typing import Optional, Tuple from fastapi import FastAPI, Request from pydantic import BaseModel # 通过starlette.authentication导入AuthenticationBac 阅读全文
posted @ 2024-02-28 18:42 我在路上回头看 阅读(397) 评论(0) 推荐(0)
摘要: 官网 sqlmodel 安装 # 安装sqlmodel会自动安装pydantic和sqlalchemy pip install sqlmodel 使用 # 步骤1,创建sqlmodel引擎 from sqlmodel import create_engine # driver://用户名:密码@ip 阅读全文
posted @ 2024-02-28 18:37 我在路上回头看 阅读(4462) 评论(0) 推荐(0)
摘要: 应用启动和关闭事件(旧版本) 事件处理程序,在应用程序启动之前和关闭期间执行。每次uvicorn和hypercorn服务器重启加载时都会激活这些事件 app = FastAPI() # 启动事件 @app.on_event("startup") async def initialize(reques 阅读全文
posted @ 2024-02-28 18:14 我在路上回头看 阅读(898) 评论(0) 推荐(0)
摘要: 依赖包 pip install python-dotenv 使用 #.env文件 ADMIN_EMAIL="deadpool@example.com" APP_NAME="ChimichangApp" # config.py from pydantic_settings import BaseSet 阅读全文
posted @ 2024-02-28 18:09 我在路上回头看 阅读(439) 评论(0) 推荐(0)
摘要: 函数式依赖项 from fastapi import FastAPI from fastapi import Query, Depends from fastapi.exceptions import HTTPException app = FastAPI() def username_check( 阅读全文
posted @ 2024-02-28 18:07 我在路上回头看 阅读(380) 评论(0) 推荐(0)
摘要: 注:后台任务应附加到响应中,并且仅在发送响应后运行 用于将单个后台任务添加到响应中 from fastapi import FastAPI from fastapi.responses import JSONResponse from starlette.background import Back 阅读全文
posted @ 2024-02-28 17:58 我在路上回头看 阅读(298) 评论(0) 推荐(0)
摘要: 中间件介绍 中间件是一个函数,它在每个请求被特定的路径操作处理之前 ,以及在每个响应返回之前工作 装饰器版中间件 1.必须使用装饰器@app.middleware("http"),且middleware_type必须为http 2.中间件参数:request, call_next,且call_nex 阅读全文
posted @ 2024-02-28 17:56 我在路上回头看 阅读(1531) 评论(0) 推荐(0)
摘要: 上传文件 # file仅适用于小文件 @app.post("/files/") async def create_file(file: bytes | None = File(default=None)): if not file: return {"message": "No file sent" 阅读全文
posted @ 2024-02-28 17:43 我在路上回头看 阅读(443) 评论(0) 推荐(0)
摘要: 模型基本用法 from pydantic import BaseModel class Item(BaseModel): # 通过继承BaseModel name: str price: float is_offer: Union[bool, None] = None 常用的模型属性和方法 dict 阅读全文
posted @ 2024-02-28 17:42 我在路上回头看 阅读(400) 评论(0) 推荐(0)
摘要: 将对象转为json兼容类型 from fastapi.encoders import jsonable_encoder # jsonable_encoder编码器 class User(BaseModel): uname: str date_signed: Optional[datetime] = 阅读全文
posted @ 2024-02-28 17:36 我在路上回头看 阅读(159) 评论(0) 推荐(0)
摘要: 设置响应状态码 from fastapi import status from fastapi.responses import JSONResponse # 方式一 @router.get("/user", status_code=status.HTTP_202_ACCEPTED) def use 阅读全文
posted @ 2024-02-28 17:35 我在路上回头看 阅读(82) 评论(0) 推荐(0)
摘要: 管理Cookie # 设置 @app.post("/create/") def create_cookie(response: Response): # 设置cookie对应的key-value值 response.set_cookie(key="name", value="jack") respo 阅读全文
posted @ 2024-02-28 17:35 我在路上回头看 阅读(97) 评论(0) 推荐(0)
摘要: 表单数据Form class User(BaseModel): uname: str password: str @app.post("/form", response_model=User) def post_form(uname: str = Form(default=None), passwo 阅读全文
posted @ 2024-02-28 17:30 我在路上回头看 阅读(113) 评论(0) 推荐(0)
摘要: 请求对象Request相关属性和函数 # request属性和函数 app 表示当前请求所属的上下文应用对象 url 表示当前请求完整的url对象 base_url 表示请求的服务url地址 method 表示此次请求使用的http方式 client 包含当前请求来源的客户端信息 Cookies 表 阅读全文
posted @ 2024-02-28 17:26 我在路上回头看 阅读(202) 评论(0) 推荐(0)
摘要: 请求头Header @app.get("/header") async def read_items(x_token: Optional[str] = Header(None, convert_underscores=True), host: Optional[str] = Header(None) 阅读全文
posted @ 2024-02-28 17:25 我在路上回头看 阅读(169) 评论(0) 推荐(0)
摘要: 请求体中的单一值Body from typing import Annotated from fastapi import Body,FastAPI # Body()表示嵌入请求体中的单一值 from pydantic import BaseModel app = FastAPI() class I 阅读全文
posted @ 2024-02-28 17:24 我在路上回头看 阅读(194) 评论(0) 推荐(0)
摘要: 请求体映射 from typing import Union from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str price: floa 阅读全文
posted @ 2024-02-28 17:22 我在路上回头看 阅读(51) 评论(0) 推荐(0)
摘要: 设置默认值 @app.get("/items/") async def read_items(q: Union[str, None] = Query(default=None, max_length=50)): # Query适用于查询参数中,设置默认值为None,并且规定最大长度为50 resul 阅读全文
posted @ 2024-02-28 17:17 我在路上回头看 阅读(75) 评论(0) 推荐(0)
摘要: 单个查询字符串 @app.get('/index/{username}') def index(username: str, id: int): # id为查询字符串 ?id=5 return {"message": "success", "username": username, "id": id 阅读全文
posted @ 2024-02-28 17:15 我在路上回头看 阅读(50) 评论(0) 推荐(0)
摘要: 路径参数额外校验Path from fastapi import Path app = FastAPI() @app.get('/items/{item_id}') async def read_items(item_id: str = Path(default=None, max_length=3 阅读全文
posted @ 2024-02-28 17:12 我在路上回头看 阅读(59) 评论(0) 推荐(0)
摘要: 路径参数 # 单一参数 @app.get('/index/{id}') def index(id: int): return {"message": "success", "data": id} # 多参数 @app.get('/index/{username}/{id}') def index(u 阅读全文
posted @ 2024-02-28 16:55 我在路上回头看 阅读(28) 评论(0) 推荐(0)
摘要: mount应用挂载 1.创建主app应用对象实例,注册所属的路由信息 from fastapi import FastAPI from fastapi.response import JSONResponse app = FastAPI(title='主应用', description='主应用描述 阅读全文
posted @ 2024-02-28 16:54 我在路上回头看 阅读(540) 评论(0) 推荐(0)
摘要: APIRouter实例的路由注册 API端点路由注册大致分为3种: 1.基于app实例对象提供的装饰器或函数进行注册 2.基于FastAPI提供的APIRouter类的实例对象提供的装饰器或函数进行注册 3.通过直接实例化APIRoute对象且添加的方式进行注册 路由注册方式 基于APIRouter 阅读全文
posted @ 2024-02-28 16:52 我在路上回头看 阅读(323) 评论(0) 推荐(0)
摘要: APIRouter和 APIRoute的区别 # APIRouter 主要是定义路由组,可以理解为一个路由组的根路由,类似于flask的蓝图 # APIRoute 表示具体路由节点对象 阅读全文
posted @ 2024-02-28 16:51 我在路上回头看 阅读(157) 评论(0) 推荐(0)
摘要: APIRouter参数介绍 class APIRouter(routing.Router): def __init__( self, *, prefix: str = "", # 表示当前路由分组的url前缀 tags: Optional[List[Union[str, Enum]]] = None 阅读全文
posted @ 2024-02-28 16:49 我在路上回头看 阅读(501) 评论(0) 推荐(0)
摘要: 一个路由配置多个http请求方法 app = FastAPI(routes=None) # 方式一:app.api_route() @app.api_route(path='/index', methods=['GET','POST']) async def index(): return {'ms 阅读全文
posted @ 2024-02-28 16:48 我在路上回头看 阅读(428) 评论(0) 推荐(0)
摘要: 节点元数据参数说明 # 拿app.get()方法的参数来说明,其他的差不多类似 def get( self, path: str, *, response_model: Optional[Type[Any]] = None, status_code: Optional[int] = None, ta 阅读全文
posted @ 2024-02-28 16:46 我在路上回头看 阅读(99) 评论(0) 推荐(0)
摘要: 全局routes参数的使用 from fastapi import FastAPI, Request from fastapi.response import JSONResponse from fastapi.routing import APIRoute async def fastapi_in 阅读全文
posted @ 2024-02-28 16:44 我在路上回头看 阅读(142) 评论(0) 推荐(0)
摘要: 如何关闭交互式文档 from fastapi import FastAPI app = FastAPI( docs_url=None, redoc_url=None, openapi_url=None, ) 阅读全文
posted @ 2024-02-28 16:43 我在路上回头看 阅读(325) 评论(0) 推荐(0)
摘要: 开启调试模式 from fastapi import FastAPI from fastapi.responses import PlainTextResponse app = FastAPI(debug=True) # 生产环境关闭 @app.get('/') def index(): 1988/ 阅读全文
posted @ 2024-02-28 16:41 我在路上回头看 阅读(579) 评论(0) 推荐(0)
摘要: FastAPI类参数说明 def __init__( self, *, debug: bool = False, # 是否启动调试模式 routes: Optional[List[BaseRoute]] = None, #自定义路由列表 title: str = "FastAPI", # api文档 阅读全文
posted @ 2024-02-28 16:40 我在路上回头看 阅读(367) 评论(0) 推荐(0)
摘要: 安装 pip3 install fastapi pip3 install uvicorn[standard] # 是一个ASGI异步服务器网关接口服务器框架 pip3 install python-multipart #处理表单参数的 # 完整安装 pip install fastapi[all] 阅读全文
posted @ 2024-02-28 16:39 我在路上回头看 阅读(366) 评论(0) 推荐(0)