flask、flask-restful、fastAPI




flask-Restful 不推荐

csharp 复制代码
from flask_restful import Resource, reqparse

parser = reqparse.RequestParser()
parser.add_argument('username', type=str, required=True, help='Username required')
parser.add_argument('age', type=int, required=True, help='Age required')

class Register(Resource):
    def post(self):
        args = parser.parse_args()
        # 手动校验长度和范围
        if not (3 <= len(args['username']) <= 20):
            return {'error': 'Username length must be 3-20'}, 400
        if not (0 <= args['age'] <= 150):
            return {'error': 'Age must be 0-150'}, 400
        # 还要手动集成文档(比如用 flask-swagger),代码分散在多处
        return {'message': f'Welcome {args["username"]}'}

Fast-api 推荐

csharp 复制代码
from fastapi import FastAPI
from pydantic import BaseModel, Field

app = FastAPI()

class UserReg(BaseModel):
    username: str = Field(..., min_length=3, max_length=20)
    age: int = Field(..., ge=0, le=150)

@app.post('/register')
def register(user: UserReg):
    return {'message': f'Welcome {user.username}'}
相关推荐
爱写代码的倒霉蛋1 天前
FastAPI的接口实现流程讲解
fastapi
ドロロ8061 天前
openAPI与Restful API的区别
后端·restful
在世修行2 天前
干货:文件上传端点解析
python·fastapi·接收上传
云和数据.ChenGuang2 天前
JAVAEE AI工程师路线图
java·人工智能·java-ee·fastapi·springai·langchain4j
风早爽太3 天前
Python 学习笔记:SQLModel 使用外键关联查询数据
python·fastapi·sqlmodel
昔我往昔3 天前
Blueprint和route的区别
flask
风早爽太3 天前
Python 学习笔记:数据库迁移工具 ‌Alembic
数据库·python·fastapi·alembic
专业程序开发源3 天前
flask动漫推荐系统32319-计算机课程设计、毕业设计
java·spring boot·后端·django·flask·php·课程设计
Web3&Basketball4 天前
用FastAPI+Redis 复刻常驻Agent
redis·bootstrap·fastapi
qq_2518364575 天前
理发管理系统 —— 会员模块
开发语言·前端·python·flask