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}'}
相关推荐
青 春 记 忆5 小时前
零基础入门python19:Flask账本第一步——应用工厂、蓝图和健康检查
python·flask·后端开发
青 春 记 忆11 小时前
零基础入门python23:Flask-Login登录、退出与会话
python·flask·后端开发
大模型丫丫15 小时前
FastAPI 入门指南:从零开始构建高性能 Python API
开发语言·python·fastapi
叫我:松哥15 小时前
基于flask图书数据管理系统,技术栈flask+MySQL+boostrap
python·mysql·flask
卷无止境15 小时前
Python的contextlib与 FastAPI 中的上下文管理
后端·python·fastapi
卷无止境15 小时前
FastAPI Events 深度解析与工程实践
后端·python·fastapi
人机(未转人工)17 小时前
Flask + MySQL + Ollama(DeepSeek) 漏洞资产库 的网络安全资产分析平台的templates中(chat.html)渲染文件
mysql·flask·html
叫我:松哥19 小时前
基于flask图书管理系统,技术栈flask+layui+sqlite+Echarts
后端·python·数据分析·flask·echarts·layui
青 春 记 忆1 天前
零基础入门python20:Flask配置、扩展和蓝图拆分
python·flask·后端开发
一只旭宝2 天前
预约系统版本2(pyhton+flask可视化版本)
服务器·数据库·c++·笔记·python·flask