03 2023 档案

摘要:''' 异步迭代器 ''' import asyncio class MyRange: def __init__(self, total=0): self.total = total self.count = 0 def __aiter__(self): return self async def 阅读全文
posted @ 2023-03-31 10:40 LeoShi2020 阅读(40) 评论(0) 推荐(0) 编辑
摘要:''' 异步上下文管理 ''' import asyncio class ContextManager: def __init__(self): self.conn = None async def action(self): return self.conn async def __aenter_ 阅读全文
posted @ 2023-03-31 10:34 LeoShi2020 阅读(24) 评论(0) 推荐(0) 编辑
摘要:''' 同步上下文管理器 ''' import time class ContextManager: def __init__(self): self.conn = None def action(self): return self.conn def __enter__(self): # 链接数据 阅读全文
posted @ 2023-03-31 10:25 LeoShi2020 阅读(14) 评论(0) 推荐(0) 编辑
摘要:import asyncio import time from concurrent.futures import ThreadPoolExecutor def download_img(url): print(f"下载图片:{url}") time.sleep(1) print(f"下载完成:{u 阅读全文
posted @ 2023-03-31 10:12 LeoShi2020 阅读(90) 评论(0) 推荐(0) 编辑
摘要:import time from concurrent.futures import ThreadPoolExecutor def download_img(url): print(f"下载图片:{url}") time.sleep(1) print(f"下载完成:{url}") def main( 阅读全文
posted @ 2023-03-31 10:08 LeoShi2020 阅读(30) 评论(0) 推荐(0) 编辑
摘要:''' concurrent.futures 模块提供异步执行可调用对象高层接口,使用线程池 ThreadPoolExecutor 或 进程池 ProcessPoolExecutor 来实现异步。目的是保证服务稳定运行的前提下提供最大的并发能力。 ''' from concurrent.future 阅读全文
posted @ 2023-03-31 09:20 LeoShi2020 阅读(21) 评论(0) 推荐(0) 编辑
摘要:import asyncio from asyncio import Future async def f1(): print(1) await asyncio.sleep(3) print(2) return "f1" def callback(f: Future): f.get_loop().s 阅读全文
posted @ 2023-03-31 09:09 LeoShi2020 阅读(68) 评论(0) 推荐(0) 编辑
摘要:import asyncio from functools import partial from asyncio import Future async def f1(): print(1) await asyncio.sleep(2) print(2) return "f1" def callb 阅读全文
posted @ 2023-03-30 19:19 LeoShi2020 阅读(185) 评论(0) 推荐(0) 编辑
摘要:相同点: 从功能上看, asyncio.wait 和 asyncio.gather 实现的效果是相同的。 不同点: 使用方式不一样,wait需要传一个可迭代对象,gather传多个元素 wait比gather更底层,gather可以直接得到结果,wait先得到future再得到结果 wait可以通过 阅读全文
posted @ 2023-03-30 17:25 LeoShi2020 阅读(168) 评论(0) 推荐(0) 编辑
摘要:import asyncio async def f1(): print(1) await asyncio.sleep(2) print(2) async def f2(): print(3) await asyncio.sleep(2) print(4) async def main(): pri 阅读全文
posted @ 2023-03-30 16:54 LeoShi2020 阅读(27) 评论(0) 推荐(0) 编辑
摘要:import asyncio import aiohttp async def download_img(session, url): file_name = url.rsplit('/')[-1] print(f"下载图片:{file_name}") await asyncio.sleep(2) 阅读全文
posted @ 2023-03-30 15:58 LeoShi2020 阅读(26) 评论(0) 推荐(0) 编辑
摘要:import asyncio async def f1(): print(1) await asyncio.sleep(2) print(2) async def f2(): print(3) await asyncio.sleep(2) # 等 可以等待的对象 print(4) tasks = [ 阅读全文
posted @ 2023-03-30 15:34 LeoShi2020 阅读(23) 评论(0) 推荐(0) 编辑
摘要:from fastapi import FastAPI from fastapi.responses import RedirectResponse app = FastAPI() @app.get("/") def go_to_baidu(): return RedirectResponse("h 阅读全文
posted @ 2023-03-30 14:21 LeoShi2020 阅读(162) 评论(0) 推荐(0) 编辑
摘要:1. StreamingResponse支持文件类型的操作 from fastapi import FastAPI from fastapi.responses import StreamingResponse app = FastAPI() @app.get("/") def index(): d 阅读全文
posted @ 2023-03-30 14:20 LeoShi2020 阅读(582) 评论(0) 推荐(0) 编辑
摘要:1.PlainTextResponse用来响应纯文本的数据 from fastapi import FastAPI from fastapi.responses import PlainTextResponse app = FastAPI() @app.get("/", response_class 阅读全文
posted @ 2023-03-30 14:17 LeoShi2020 阅读(164) 评论(0) 推荐(0) 编辑
摘要:1. 文件结构 ├── main.py └── routers ├── blog.py └── user.py 2. blog.py from fastapi import APIRouter router = APIRouter() @router.get("/blogs") def blogs( 阅读全文
posted @ 2023-03-30 13:40 LeoShi2020 阅读(152) 评论(0) 推荐(0) 编辑
摘要:# 杀死带有‘python’关健字的所有进程 # grep -v 过滤掉 'color'关健字的进程 kill -9 `ps -aux | grep python | grep -v color | awk '{print $2}'` 阅读全文
posted @ 2023-03-30 09:51 LeoShi2020 阅读(16) 评论(0) 推荐(0) 编辑
摘要:from gmssl import sm4, sm3 def sm3_hash(message: str): """ 国密sm3加密 :param message: 消息值,bytes类型 :return: 哈希值 """ msg_list = [i for i in bytes(message.e 阅读全文
posted @ 2023-03-29 20:26 LeoShi2020 阅读(2223) 评论(0) 推荐(0) 编辑
摘要:代码结构 ── sql_app ├── main.py # 程序入口 ├── crud.py # 增删查改的方法 ├── database.py # 数据库相关 ├── models.py # ORM模型类相关 └── schemas.py # Pydantic的BaseModel,校验相关 阅读全文
posted @ 2023-03-28 20:51 LeoShi2020 阅读(35) 评论(0) 推荐(0) 编辑
摘要:import typing from fastapi import FastAPI, HTTPException, Depends import pymysql from sqlalchemy import create_engine, Column, String, Integer from sq 阅读全文
posted @ 2023-03-28 20:37 LeoShi2020 阅读(179) 评论(0) 推荐(0) 编辑
摘要:import typing from fastapi import FastAPI, HTTPException, Depends import pymysql from sqlalchemy import create_engine, Column, String, Integer from sq 阅读全文
posted @ 2023-03-28 19:21 LeoShi2020 阅读(86) 评论(0) 推荐(0) 编辑
摘要:import pymysql from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy import Column, String, Integer from sqlalc 阅读全文
posted @ 2023-03-28 18:45 LeoShi2020 阅读(366) 评论(0) 推荐(0) 编辑
摘要:import pymysql from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy import Column, String, Integer from sqlalc 阅读全文
posted @ 2023-03-28 09:51 LeoShi2020 阅读(351) 评论(0) 推荐(0) 编辑
摘要:import pymysql from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy import Column, String, Integer from sqlalc 阅读全文
posted @ 2023-03-28 09:33 LeoShi2020 阅读(353) 评论(0) 推荐(0) 编辑
摘要: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 阅读(281) 评论(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 阅读(105) 评论(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 阅读(45) 评论(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 阅读(172) 评论(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 阅读(16) 评论(0) 推荐(0) 编辑
摘要:1. 手动实现 ''' - 进入with语句块时,就会执行文件类的`__enter__`返回一个文件对象,并赋值给变量 `f` - 从with语句块出来时,机会执行文件类的`__exit__`,在其内部实现 f.close(),所以使用者就不需要在手动关闭文件对象了。 ''' class MyFil 阅读全文
posted @ 2023-03-26 22:22 LeoShi2020 阅读(20) 评论(0) 推荐(0) 编辑
摘要:from fastapi import Depends, FastAPI app = FastAPI(title="依赖注入yield", description="在路径操作函数结束时,会自动关闭db连接回收资源。及时在路径函数会出现异常报错,最终也会关闭连接。") def get_db(): d 阅读全文
posted @ 2023-03-26 22:14 LeoShi2020 阅读(24) 评论(0) 推荐(0) 编辑
摘要:from fastapi import Depends, FastAPI app = FastAPI(title="基于对象的依赖注入", description="检查指定的文本是否在查询参数q中") class FixedContentQueryChecker: def __init__(sel 阅读全文
posted @ 2023-03-26 21:59 LeoShi2020 阅读(35) 评论(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 阅读(34) 评论(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 阅读(126) 评论(0) 推荐(0) 编辑
摘要:from fastapi import Depends, FastAPI app = FastAPI() ''' 依赖注入缓存现象 - 依赖条件`get_num`被依赖了两次,但是你会发现其内部打印语句只打印了一次。也就是说, 第二次使用这个依赖条件时FastAPI并没有真正执行这个函数,而是直接使 阅读全文
posted @ 2023-03-26 20:46 LeoShi2020 阅读(187) 评论(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 阅读(30) 评论(0) 推荐(0) 编辑
摘要:from fastapi import FastAPI, Depends app = FastAPI(title="依赖注入") ''' 依赖注入 - 共享一块相同逻辑的代码块 - 共享数据库连接 - 权限认证,登录状态认证 ''' BOOKS = [{"id": i, "name": f"book 阅读全文
posted @ 2023-03-26 20:02 LeoShi2020 阅读(76) 评论(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 阅读(21) 评论(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 阅读(50) 评论(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 阅读(86) 评论(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 阅读(182) 评论(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 阅读(14) 评论(0) 推荐(0) 编辑
摘要:import typing import json from fastapi import FastAPI, Response from fastapi.encoders import jsonable_encoder from datetime import datetime from pydan 阅读全文
posted @ 2023-03-25 17:41 LeoShi2020 阅读(169) 评论(0) 推荐(0) 编辑
摘要:import typing from fastapi import FastAPI, Response from fastapi.responses import JSONResponse from pydantic import BaseModel app = FastAPI() ''' pyda 阅读全文
posted @ 2023-03-25 17:25 LeoShi2020 阅读(96) 评论(0) 推荐(0) 编辑
摘要:import typing from fastapi import FastAPI, Response from fastapi.responses import JSONResponse from pydantic import BaseModel app = FastAPI() ''' 过滤响应 阅读全文
posted @ 2023-03-25 15:45 LeoShi2020 阅读(130) 评论(0) 推荐(0) 编辑
摘要:import typing from fastapi import FastAPI, Response from fastapi.responses import JSONResponse from pydantic import BaseModel app = FastAPI() ''' 响应模型 阅读全文
posted @ 2023-03-25 12:40 LeoShi2020 阅读(89) 评论(0) 推荐(0) 编辑
摘要:from fastapi import FastAPI, Response from fastapi.responses import JSONResponse from pydantic import BaseModel app = FastAPI() class User(BaseModel): 阅读全文
posted @ 2023-03-25 12:17 LeoShi2020 阅读(21) 评论(0) 推荐(0) 编辑
摘要:from fastapi import FastAPI, Response from fastapi.responses import JSONResponse from pydantic import BaseModel app = FastAPI() class User(BaseModel): 阅读全文
posted @ 2023-03-25 11:12 LeoShi2020 阅读(130) 评论(0) 推荐(0) 编辑
摘要:from fastapi import FastAPI,status from pydantic import BaseModel app = FastAPI() ''' 响应状态代码 status ''' class User(BaseModel): username : str password 阅读全文
posted @ 2023-03-24 11:30 LeoShi2020 阅读(21) 评论(0) 推荐(0) 编辑
摘要:import random from fastapi import FastAPI from pydantic import Field, BaseModel import typing app = FastAPI() ''' 请求体的每一个字段需要单独校验 name 长度最少3位 price 不少 阅读全文
posted @ 2023-03-24 10:51 LeoShi2020 阅读(20) 评论(0) 推荐(0) 编辑
摘要:from fastapi import FastAPI, Body from pydantic import BaseModel import typing app = FastAPI() ''' { "name": "book", "description": "python", "price": 阅读全文
posted @ 2023-03-24 00:43 LeoShi2020 阅读(22) 评论(0) 推荐(0) 编辑
摘要:from fastapi import FastAPI,Body app =FastAPI() ''' 使用方法同Path Query ''' @app.post("/login") def login( name :str = Body(min_length=3), age:int = Body( 阅读全文
posted @ 2023-03-24 00:17 LeoShi2020 阅读(60) 评论(0) 推荐(0) 编辑
摘要:from fastapi import FastAPI, Body from pydantic import BaseModel app = FastAPI() ''' 使用Body接收请求体数据 { "user": { "username": "Tom", "password": "1234657 阅读全文
posted @ 2023-03-24 00:05 LeoShi2020 阅读(362) 评论(0) 推荐(0) 编辑
摘要:from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() ''' 多个请求体 { "user": { "username": "string", "password": "string" }, "item": 阅读全文
posted @ 2023-03-23 23:46 LeoShi2020 阅读(31) 评论(0) 推荐(0) 编辑
摘要:from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() ''' 创建继承BaseModel的类,定义模型user 路径函数中定义形参user,类型为User 通过对象user的属性获取字段的值 客户端使用P 阅读全文
posted @ 2023-03-23 23:29 LeoShi2020 阅读(101) 评论(0) 推荐(0) 编辑
摘要:# 查找文件路径 find / -name docs.py vi docs.py # swagger_js_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/swagger-ui-bundle.js", swagger_js_url 阅读全文
posted @ 2023-03-23 22:38 LeoShi2020 阅读(442) 评论(1) 推荐(0) 编辑
摘要:import typing from fastapi import FastAPI,Header app = FastAPI() @app.get("/books") # 接收多个token def books(token :typing.List[str] = Header()): return 阅读全文
posted @ 2023-03-23 22:07 LeoShi2020 阅读(32) 评论(0) 推荐(0) 编辑
摘要:import typing from fastapi import FastAPI, Query app = FastAPI() ''' 查询参数使用Query校验 类似路由转换使用Path校验 物品名称最小3位,最大10位 default=None 参数为可选项,否则为必选项 default=.. 阅读全文
posted @ 2023-03-23 21:06 LeoShi2020 阅读(64) 评论(0) 推荐(0) 编辑
摘要:from fastapi import FastAPI, Query import typing app = FastAPI() ''' 查询多个参数值相同 ''' @app.get("/books") def books(book_id: typing.List[int] = Query()): 阅读全文
posted @ 2023-03-23 20:36 LeoShi2020 阅读(40) 评论(0) 推荐(0) 编辑
摘要:import typing from fastapi import FastAPI app = FastAPI() BOOKS = [ {"id": i, "title": f"book{i}"} for i in range(1, 11) ] ''' 查询参数的默认值 ''' @app.get(" 阅读全文
posted @ 2023-03-23 17:23 LeoShi2020 阅读(107) 评论(0) 推荐(0) 编辑
摘要:from fastapi import FastAPI,Path app = FastAPI() # Path校验 ''' 限制接口输入的数字大小限制 100-1000 限制字符串输入的字符数量 3-8位 ''' # deprecated 即将废弃 # include_in_schema 隐藏接口 阅读全文
posted @ 2023-03-23 16:40 LeoShi2020 阅读(29) 评论(0) 推荐(0) 编辑
摘要:from enum import Enum from fastapi import FastAPI app = FastAPI() # 路径参数枚举值 ''' 编程语言三种分类:python java go ''' # 继承str 枚举Enum class LangName(str, Enum): 阅读全文
posted @ 2023-03-23 12:35 LeoShi2020 阅读(178) 评论(0) 推荐(0) 编辑
摘要:from fastapi import FastAPI app= FastAPI() # 路径转换器 ''' - str 字符串 - int 数字 - float 浮点 - uuid 返回python中的uuid.UUID - path 文件路径包含多个/ ''' @app.get("/books/ 阅读全文
posted @ 2023-03-23 10:14 LeoShi2020 阅读(40) 评论(0) 推荐(0) 编辑
摘要:![](https://img2023.cnblogs.com/blog/1940615/202303/1940615-20230323090712489-1952166291.png) 阅读全文
posted @ 2023-03-23 09:07 LeoShi2020 阅读(24) 评论(0) 推荐(0) 编辑
摘要:from fastapi import FastAPI BOOKS = [ {"id": 1, "title": "book1"}, {"id": 2, "title": "book2"}, {"id": 3, "title": "book3"}, {"id": 4, "title": "book4 阅读全文
posted @ 2023-03-22 09:09 LeoShi2020 阅读(569) 评论(0) 推荐(0) 编辑
摘要:from fastapi import FastAPI app= FastAPI() # 静态路由优先级高于动态路由 # 必须写在动态路由的前面 @app.get("/books/most_populer") def books_most_populer(): return {"This Book 阅读全文
posted @ 2023-03-21 16:42 LeoShi2020 阅读(151) 评论(0) 推荐(0) 编辑
摘要:from fastapi import FastAPI app= FastAPI() # 静态路由模式 @app.get("/login") def login(): return {"msg":"Welcome CoCo Login"} @app.get("/books/{number}") # 阅读全文
posted @ 2023-03-21 16:29 LeoShi2020 阅读(37) 评论(0) 推荐(0) 编辑
摘要:1. console登录 账号:admin 密码:admin@huawei.com 2. 转换FAT模式 <Huawei> system-view [Huawei] ap-mode-switch fat 3. web登录 修改默认密码 4. 网络配置 SSID "向导"-》"新建" POE接口划入V 阅读全文
posted @ 2023-03-20 10:15 LeoShi2020 阅读(921) 评论(0) 推荐(0) 编辑
摘要:yum install https://packages.endpointdev.com/rhel/7/os/x86_64/endpoint-repo.x86_64.rpm yum install git 阅读全文
posted @ 2023-03-19 20:47 LeoShi2020 阅读(66) 评论(0) 推荐(0) 编辑
摘要:clock timezone UTC add 08:00:00 ntp-service enable ntp-service unicast-server 10.105.210.3 ntp-service unicast-server 10.105.210.4 dis ntp-service ses 阅读全文
posted @ 2023-03-05 12:55 LeoShi2020 阅读(96) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示