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}'}
相关推荐
阿标在干嘛12 小时前
从RESTful到GraphQL:政策快报平台接口设计的演进
后端·restful·graphql
aGdF8E3gQ15 小时前
【Application Insights】采样率对Function App日志收集的影响和解决方法
python·flask·wpf
李昊哲小课17 小时前
fastapi sse websocket 智能家居实时控制台
python·websocket·智能家居·fastapi·sse
赵广陆18 小时前
SQLAlchemy ORM 超全教程
python·fastapi
为啥全要学20 小时前
fastapi websocket全双工通信
websocket·网络协议·fastapi
编码者卢布20 小时前
【Azure APIM】通过 API Management 公开API为 MCP Server 的试验 (二)
microsoft·flask·azure
这就是佬们吗20 小时前
企业Agent落地为什么需要RAG?
人工智能·python·fastapi
鲨鱼辣钊2 天前
【FastAPI 筑基 - Day2】彩色日志封装 + Python 装饰器精讲
fastapi
Muselit2 天前
Python 泛型:把 list[User] 讲明白
python·fastapi
赵广陆2 天前
FastAPI零基础完整实战
前端·chrome·fastapi