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}'}
相关推荐
动能小子ohhh12 小时前
DocForge平台的设计与开发--文件上传接口的实现
开发语言·人工智能·python·langchain·ocr·fastapi
XGeFei17 小时前
【Fastapi学习笔记(6)】—— Fastapi文件上传、请求头自动转换
笔记·学习·fastapi
指令集梦境1 天前
Cursor + Spring Boot实战:从零写一个RESTful API
spring boot·后端·restful
li星野2 天前
从零搭建带数据库的文件上传系统:FastAPI + Streamlit + SQLite+加上日志
数据库·sqlite·fastapi
zhoupenghui1682 天前
AI大模型应用部署之Flask框架使用
运维·python·docker·容器·flask·flask框架
小小龙学IT2 天前
Go 后端开发实战:构建高性能 RESTful API 服务
开发语言·golang·restful
海鸥-w2 天前
用python (fastapi)做项目第二天实现新闻列表和新闻详情接口
开发语言·python·fastapi
曲幽2 天前
FastAPI 身份验证总踩坑?这份 FastAPI Users “避坑指南”请收好
python·fastapi·web·jwt·oauth2·user·authentication
海鸥-w3 天前
用python (fastapi)做项目第一天创建项目结构,数据建表,ORM配置安装,写第一个接口
数据库·python·fastapi
㳺三才人子3 天前
初探 Flask-WTF
后端·python·flask·html5