摘要: from urllib.parse import quote_plus as urlquote # 指定连接的MySQL数据库 PASSWORD = 'root@demo.demo' DATABASE_URL = f"mysql://root:{urlquote(PASSWORD)}@10.105. 阅读全文
posted @ 2023-03-26 23:56 LeoShi2020 阅读(142) 评论(0) 推荐(0) 编辑
摘要: import pymysql # 获取连接 conn = pymysql.connect( host='10.105.212.1', port=3306, user='root', password='DemoDemo', database='db', charset='utf8' ) cursor 阅读全文
posted @ 2023-03-26 23:25 LeoShi2020 阅读(69) 评论(0) 推荐(0) 编辑
摘要: 1. 手工新增 import pymysql # 获取连接 conn = pymysql.connect( host='10.105.212.1', port=3306, user='root', password='DemoDemo', database='db', charset='utf8' 阅读全文
posted @ 2023-03-26 23:23 LeoShi2020 阅读(28) 评论(0) 推荐(0) 编辑
摘要: import pymysql # 获取连接 conn = pymysql.connect( host='10.105.212.1', port=3306, user='root', password='root@Twitt3r.com', database='db', charset='utf8' 阅读全文
posted @ 2023-03-26 23:16 LeoShi2020 阅读(128) 评论(0) 推荐(0) 编辑
摘要: import pymysql # 获取连接 conn = pymysql.connect( host='10.105.212.1', port=3306, user='root', password='DemoDemo', database='db', charset='utf8' ) # 获取游标 阅读全文
posted @ 2023-03-26 23:08 LeoShi2020 阅读(15) 评论(0) 推荐(0) 编辑
摘要: 1. 手动实现 ''' - 进入with语句块时,就会执行文件类的`__enter__`返回一个文件对象,并赋值给变量 `f` - 从with语句块出来时,机会执行文件类的`__exit__`,在其内部实现 f.close(),所以使用者就不需要在手动关闭文件对象了。 ''' class MyFil 阅读全文
posted @ 2023-03-26 22:22 LeoShi2020 阅读(13) 评论(0) 推荐(0) 编辑
摘要: from fastapi import Depends, FastAPI app = FastAPI(title="依赖注入yield", description="在路径操作函数结束时,会自动关闭db连接回收资源。及时在路径函数会出现异常报错,最终也会关闭连接。") def get_db(): d 阅读全文
posted @ 2023-03-26 22:14 LeoShi2020 阅读(15) 评论(0) 推荐(0) 编辑
摘要: from fastapi import Depends, FastAPI app = FastAPI(title="基于对象的依赖注入", description="检查指定的文本是否在查询参数q中") class FixedContentQueryChecker: def __init__(sel 阅读全文
posted @ 2023-03-26 21:59 LeoShi2020 阅读(31) 评论(0) 推荐(0) 编辑
摘要: from fastapi import Depends, FastAPI app = FastAPI() ''' 基于类的依赖注入 ''' BOOKS = [{"id": i, "name": f"book{i}", "status": i % 4 != 0} for i in range(1, 1 阅读全文
posted @ 2023-03-26 21:41 LeoShi2020 阅读(28) 评论(0) 推荐(0) 编辑
摘要: 1. 路径装饰器 from fastapi import FastAPI, Header, HTTPException, Depends,status app = FastAPI() def verify_token(x_token: str = Header()): if x_token != " 阅读全文
posted @ 2023-03-26 21:03 LeoShi2020 阅读(99) 评论(0) 推荐(0) 编辑
摘要: from fastapi import Depends, FastAPI app = FastAPI() ''' 依赖注入缓存现象 - 依赖条件`get_num`被依赖了两次,但是你会发现其内部打印语句只打印了一次。也就是说, 第二次使用这个依赖条件时FastAPI并没有真正执行这个函数,而是直接使 阅读全文
posted @ 2023-03-26 20:46 LeoShi2020 阅读(154) 评论(0) 推荐(0) 编辑
摘要: from typing import Union from fastapi import Depends, FastAPI app = FastAPI() ''' 嵌套注入 - 路径函数get_name需要的形参`username_or_nickname`有依赖条件,所以FastAPI会调用 `us 阅读全文
posted @ 2023-03-26 20:35 LeoShi2020 阅读(21) 评论(0) 推荐(0) 编辑
摘要: from fastapi import FastAPI, Depends app = FastAPI(title="依赖注入") ''' 依赖注入 - 共享一块相同逻辑的代码块 - 共享数据库连接 - 权限认证,登录状态认证 ''' BOOKS = [{"id": i, "name": f"book 阅读全文
posted @ 2023-03-26 20:02 LeoShi2020 阅读(62) 评论(0) 推荐(0) 编辑
摘要: import typing from fastapi import FastAPI, Form, File, UploadFile from pydantic import BaseModel app = FastAPI(title="注册接口") ''' 1. 需要输入 账号 密码 头像 ''' 阅读全文
posted @ 2023-03-26 18:25 LeoShi2020 阅读(17) 评论(0) 推荐(0) 编辑
摘要: import typing from fastapi import FastAPI, File, UploadFile app = FastAPI(title="Form表单") ''' 上传多个文件 ''' @app.post("/files",summary="通过内存缓存上传多个文件") de 阅读全文
posted @ 2023-03-26 17:57 LeoShi2020 阅读(25) 评论(0) 推荐(0) 编辑
摘要: import typing from fastapi import FastAPI, File, UploadFile app = FastAPI(title="Form表单") ''' 上传文件为可选项 ''' @app.post("/upload_large_file", summary="上传 阅读全文
posted @ 2023-03-26 17:38 LeoShi2020 阅读(43) 评论(0) 推荐(0) 编辑
摘要: from fastapi import FastAPI,Form,File,UploadFile app = FastAPI(title="Form表单") ''' Form表单接收数据 ''' @app.post("/login",summary="登录") def login(username 阅读全文
posted @ 2023-03-26 17:04 LeoShi2020 阅读(139) 评论(0) 推荐(0) 编辑
摘要: import typing from fastapi import FastAPI, Query, HTTPException, status from fastapi.encoders import jsonable_encoder from pydantic import BaseModel a 阅读全文
posted @ 2023-03-26 12:51 LeoShi2020 阅读(10) 评论(0) 推荐(0) 编辑