🦋
Butterfly 开发手册
  • Butterfly
  • project-framework
    • 一 why
    • 二 what
    • 三 how
      • HTTP 协议基础
      • HTTP 服务器模型
      • WSGIGateway
      • 框架之路由系统
      • 框架之 MIDDLEWARE
      • 框架之日志管理
      • 框架之 HTTP 接口
        • 请求流程
        • handler 类型
          • (1) json
          • (2) file
          • (3) HTTP
        • API 设计风格
        • 响应格式自定义
        • cookie
      • 基础公共库之模板引擎
        • 配置文件渲染
      • 基础公共库之本地缓存
      • 数据访问层之资源访问 RAL(百度特有协议支持)
      • 调试和测试之调试
      • 调试和测试之单元测试
      • 状态机
  • project-handlers
    • 云原生
      • 可观测性
        • Tracing
    • 通用服务
      • 流程编排(星桥)
      • 任务队列(百川)
        • 异步任务
        • baichuan_go_woker
      • 认证鉴权(火眼)
      • 任务调度(如期)
      • 配置管理(五行)
      • 大盘统计(沧海)
      • tag 服务(画像)
    • 春风(DevOps)
      • 定期巡检(扁鹊)
        • 全量检查-Redis
        • 同步探测-Redis
      • 根因诊断(寻踪)
      • 故障自愈(青囊)
      • 数据分析(庖丁)
      • 应急预案(游刃)
    • 奕秋(服务管理)
      • 包管理(百仓)
    • 纵横(资源管理)
      • 资源管理
    • 磐石(基础组件)
      • MySQL
        • MySQL HA
        • MySQL replication
        • MySQL rpl_semi
        • MySQL connection
        • MySQL Client
      • Redis
        • AOF parse
        • RedisShake
        • RDB parse
        • modules
        • config repl-stream-db
      • ES
        • Elasticsearch as Database
        • Elasticsearch py
        • FAQ
      • Like Redis
        • rosedb
      • ZooKeeper
      • Etcd
        • etcd raft
      • HAProxy
        • HAProxy config
      • PostgreSQL
        • PostgreSQL client
    • 其他
      • 数据同步
      • 智能客服
      • 资源定位
      • 工单系统
      • 评论服务
      • 推荐系统
      • 混沌工程
      • 软件测试
  • project-third
    • grafana
      • ES
    • kibana
  • Project-Other
    • net/web
      • urllib2
    • 他山之石
      • django/tonado/flask/bottle
      • wsgi 和 asgi
      • pecan
    • 其他杂记
      • serverless
      • 云原生
Powered by GitBook
On this page
  1. project-framework
  2. 三 how
  3. 框架之 HTTP 接口
  4. handler 类型

(2) file

Previous(1) jsonNext(3) HTTP

Last updated 3 years ago

CtrlK
  • HTTP 接口之文件处理 -- 上传与下载
  • 1 文件下载
  • 2 文件上传
  • 传送门

HTTP 接口之文件处理 -- 上传与下载

  • 1 文件下载

  • 2 文件上传

  • 传送门

1 文件下载

文件下载使用 protocol_file.py

+ Protocol file--------------------------------+
|+ handler -----------------------------------+|
||file  :/handlers/{app}/__init__.py:{handler}||
||return:(stat_str, data_dict, headers_list)  ||
|+--------------------------------------------+|
| return:httpstatus, headers, content          |
+----------------------------------------------+

HTTP 请求方法:
    仅支持 GET 方法(仅用于获取文件)

HTTP 响应状态码及 Content-Type:
    HTTP 响应状态码:
        文件不存在时,HTTP 状态码为 404
        文件没读权限时,HTTP 状态码为 403
        检查参数错误时,HTTP 状态码为 400
        程序执行异常时,HTTP 状态码为 500
    HTTP 响应 Content-Type:

例子

from xlib.httpgateway import Request
from xlib import retstat
from xlib.middleware import funcattr

__info = "api_demo"
__version = "1.0.1"


@funcattr.api_download
def download(req):
    """
    Args:
        req             : (Object) Request
    Returns:
        stat_str, content_dict, headers_list
        > stat_str      : (String)
        > content_dict  : (Dict)
            filename    : (String) 文件路径
            is_download : (Bool) 是否需要下载
        > headers_list  : (List)
            key 和 value 都需要是 str 类型
    """
    isinstance(req, Request)
    return retstat.OK, {"filename": "test/static_file/test_html.html", "is_download": True}, [(__info, __version)]

2 文件上传

文件上传使用 protocol_json.py

+ Protocol json--------------------------------+
|+ handler -----------------------------------+|
||file  :/handlers/{app}/__init__.py:{handler}||
||return:(stat_str, data_dict, headers_list)  ||
|+--------------------------------------------+|
| return:httpstatus, headers, content          |
+----------------------------------------------+

HTTP 请求方法

HTTP 响应状态码及 Content-Type:
    当需要序列化为 JSON 时, HTTP 状态码均为 200, 响应内容类型均为 ("Content-Type", "application/json"):
        检查参数错误时,返回 {"stat": "ERR_BAD_PARAMS"}
        程序执行异常时,返回 {"stat": "ERR_SERVER_EXCEPTION"}

例子

from xlib.httpgateway import Request
from xlib import retstat
from xlib.middleware import funcattr

__info = "api_demo"
__version = "1.0.1"


@funcattr.api_upload
def upload(req):
    """
    
    """
    # 处理请求 body 
    return retstat.OK, {}, []

传送门

  • HTTP Content-Type 及 Python mimetypes 库

  • 如何流式读取数 G 超大文件

文件 is_download 为 True 时,会根据文件后缀名进行识别文件类型
文件 is_download 为 False 时,响应内容类型均为 ("Content-Type", "text/html")