摘要: HttpBasic基本认证 from fastapi import FastAPI, Depends from fastapi.security import HTTPBasic, HTTPBasicCredentials from fastapi.exceptions import HTTPExc 阅读全文
posted @ 2024-02-28 19:09 我在路上回头看 阅读(80) 评论(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 我在路上回头看 阅读(622) 评论(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 我在路上回头看 阅读(276) 评论(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 我在路上回头看 阅读(109) 评论(0) 推荐(0) 编辑
摘要: 官网 sqlmodel 安装 # 安装sqlmodel会自动安装pydantic和sqlalchemy pip install sqlmodel 使用 # 步骤1,创建sqlmodel引擎 from sqlmodel import create_engine # driver://用户名:密码@ip 阅读全文
posted @ 2024-02-28 18:37 我在路上回头看 阅读(1325) 评论(0) 推荐(0) 编辑
摘要: 应用启动和关闭事件(旧版本) 事件处理程序,在应用程序启动之前和关闭期间执行。每次uvicorn和hypercorn服务器重启加载时都会激活这些事件 app = FastAPI() # 启动事件 @app.on_event("startup") async def initialize(reques 阅读全文
posted @ 2024-02-28 18:14 我在路上回头看 阅读(236) 评论(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 我在路上回头看 阅读(169) 评论(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 我在路上回头看 阅读(91) 评论(0) 推荐(0) 编辑
摘要: 注:后台任务应附加到响应中,并且仅在发送响应后运行 用于将单个后台任务添加到响应中 from fastapi import FastAPI from fastapi.responses import JSONResponse from starlette.background import Back 阅读全文
posted @ 2024-02-28 17:58 我在路上回头看 阅读(116) 评论(0) 推荐(0) 编辑
摘要: 中间件介绍 中间件是一个函数,它在每个请求被特定的路径操作处理之前 ,以及在每个响应返回之前工作 装饰器版中间件 1.必须使用装饰器@app.middleware("http"),且middleware_type必须为http 2.中间件参数:request, call_next,且call_nex 阅读全文
posted @ 2024-02-28 17:56 我在路上回头看 阅读(553) 评论(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 我在路上回头看 阅读(170) 评论(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 我在路上回头看 阅读(117) 评论(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 我在路上回头看 阅读(63) 评论(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 我在路上回头看 阅读(30) 评论(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 我在路上回头看 阅读(54) 评论(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 我在路上回头看 阅读(54) 评论(0) 推荐(0) 编辑
摘要: 请求对象Request相关属性和函数 # request属性和函数 app 表示当前请求所属的上下文应用对象 url 表示当前请求完整的url对象 base_url 表示请求的服务url地址 method 表示此次请求使用的http方式 client 包含当前请求来源的客户端信息 Cookies 表 阅读全文
posted @ 2024-02-28 17:26 我在路上回头看 阅读(99) 评论(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 我在路上回头看 阅读(59) 评论(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 我在路上回头看 阅读(96) 评论(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 我在路上回头看 阅读(18) 评论(0) 推荐(0) 编辑