litefs.handlers package

class litefs.handlers.EnhancedRequestHandler(request_handler)[源代码]

基类:object

增强的请求处理器

提供分离的 query 和 post 参数,以及表单验证功能

property body: str

获取请求体

返回:

请求体字符串

property config

获取配置对象

返回:

配置对象

property content_length: int

获取内容长度

返回:

内容长度

property content_type: str | None

获取内容类型

返回:

内容类型

property cookie

获取 Cookie

返回:

Cookie 对象

property environ: Dict[str, Any]

获取环境变量

返回:

环境变量字典

property files: Dict[str, Any]

获取上传的文件

返回:

文件字典

get_file(key: str, default: Any | None = None) Any[源代码]

获取单个上传文件

参数:
  • key -- 文件字段名

  • default -- 默认值

返回:

文件对象

get_post_param(key: str, default: Any | None = None) Any[源代码]

获取单个 POST 参数

参数:
  • key -- 参数名

  • default -- 默认值

返回:

参数值

get_query_param(key: str, default: Any | None = None) Any[源代码]

获取单个查询参数

参数:
  • key -- 参数名

  • default -- 默认值

返回:

参数值

property json: Dict[str, Any]

获取 JSON 请求体

返回:

JSON 数据字典

property path_info: str

获取路径信息

返回:

路径信息

property post: Dict[str, Any]

获取 POST 请求体参数

返回:

POST 参数字典

property query: Dict[str, Any]

获取 URL 查询参数

返回:

查询参数字典

property query_string: str

获取查询字符串

返回:

查询字符串

property referer: str | None

获取来源页面

返回:

来源页面 URL

property request_method: str

获取请求方法

返回:

请求方法(GET, POST, PUT, DELETE 等)

property request_uri: str

获取请求 URI

返回:

请求 URI

property session

获取会话对象

返回:

会话对象

property session_id: str | None

获取会话 ID

返回:

会话 ID

设置 Cookie

参数:
  • key -- Cookie 名称

  • value -- Cookie 值

  • **kwargs -- 其他 Cookie 参数

set_form_validator(validator: FormValidator)[源代码]

设置表单验证器

参数:

validator -- 表单验证器实例

start_response(status_code: int = 200, headers=None)[源代码]

开始响应

参数:
  • status_code -- HTTP 状态码

  • headers -- 响应头列表

validate_all(query_rules: Dict[str, List] | None = None, post_rules: Dict[str, List] | None = None, file_rules: Dict[str, List] | None = None) Tuple[bool, Dict[str, Dict[str, List[str]]]][源代码]

验证所有参数

参数:
  • query_rules -- 查询参数验证规则

  • post_rules -- POST 参数验证规则

  • file_rules -- 文件验证规则

返回:

错误字典})

返回类型:

(是否验证成功, {参数类型

validate_files(rules: Dict[str, List]) Tuple[bool, Dict[str, List[str]]][源代码]

验证上传文件

参数:

rules -- 验证规则字典 {字段名: [验证器列表]}

返回:

(是否验证成功, 错误字典)

validate_post(rules: Dict[str, List]) Tuple[bool, Dict[str, List[str]]][源代码]

验证 POST 参数

参数:

rules -- 验证规则字典 {字段名: [验证器列表]}

返回:

(是否验证成功, 错误字典)

validate_query(rules: Dict[str, List]) Tuple[bool, Dict[str, List[str]]][源代码]

验证查询参数

参数:

rules -- 验证规则字典 {字段名: [验证器列表]}

返回:

(是否验证成功, 错误字典)

class litefs.handlers.RequestHandler(app, rw, environ, request)[源代码]

基类:BaseRequestHandler

property body
property charset
property config
property content_length
property content_type
property cookie
property data
default_headers = {'Content-Type': 'application/json; charset=utf-8', 'Server': 'litefs/0.4.0', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'SAMEORIGIN', 'X-XSS-Protection': '1; mode=block'}
property environ
property files
finish(content)[源代码]
handle_response(result)[源代码]

处理响应结果,支持 Response 对象

handler()[源代码]
property json
property method
property params
property path_info
property query_string
redirect(url=None)[源代码]
property referer
property request_method
property request_uri
property server_protocol
property session
property session_id
class litefs.handlers.Response(content=None, status_code=200, headers=None)[源代码]

基类:object

响应对象,提供更丰富的响应方法

classmethod error(status_code, message=None, headers=None)[源代码]

返回错误响应

classmethod file(file_path, status_code=200, headers=None)[源代码]

返回文件响应

classmethod html(content, status_code=200, headers=None)[源代码]

返回 HTML 响应

classmethod json(data, status_code=200, headers=None)[源代码]

返回 JSON 响应

classmethod redirect(url, status_code=302, headers=None)[源代码]

返回重定向响应

classmethod text(content, status_code=200, headers=None)[源代码]

返回纯文本响应

class litefs.handlers.WSGIRequestHandler(app, environ)[源代码]

基类:BaseRequestHandler

WSGI 请求处理器,用于在 gunicorn、uWSGI 等 WSGI 服务器中运行

符合 PEP 3333 规范,处理 WSGI environ 并返回标准响应

property body
property charset
property config
property content_length
property content_type
property cookie
property data
property environ
property files
handler()[源代码]
property json
property method
property params
property path_info
property query_string
property referer
property request_method
property request_uri
property server_protocol
property session
property session_id

Submodules

litefs.handlers.request module

class litefs.handlers.request.BaseRequestHandler(app, environ)[源代码]

基类:object

请求处理器基类,提供通用的请求处理功能

property app

获取应用实例

返回:

应用实例

property body
property files
property form

表单数据,与 post 属性相同

返回:

表单数据字典

property get
property post
render_template(template_name, **kwargs)[源代码]

渲染模板

参数:
  • template_name -- 模板文件名

  • **kwargs -- 模板变量

返回:

渲染后的 HTML 字符串

property session
property session_id
start_response(status_code=200, headers=None)[源代码]
class litefs.handlers.request.RequestHandler(app, rw, environ, request)[源代码]

基类:BaseRequestHandler

property body
property charset
property config
property content_length
property content_type
property cookie
property data
default_headers = {'Content-Type': 'application/json; charset=utf-8', 'Server': 'litefs/0.4.0', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'SAMEORIGIN', 'X-XSS-Protection': '1; mode=block'}
property environ
property files
finish(content)[源代码]
handle_response(result)[源代码]

处理响应结果,支持 Response 对象

handler()[源代码]
property json
property method
property params
property path_info
property query_string
redirect(url=None)[源代码]
property referer
property request_method
property request_uri
property server_protocol
property session
property session_id
class litefs.handlers.request.Response(content=None, status_code=200, headers=None)[源代码]

基类:object

响应对象,提供更丰富的响应方法

classmethod error(status_code, message=None, headers=None)[源代码]

返回错误响应

classmethod file(file_path, status_code=200, headers=None)[源代码]

返回文件响应

classmethod html(content, status_code=200, headers=None)[源代码]

返回 HTML 响应

classmethod json(data, status_code=200, headers=None)[源代码]

返回 JSON 响应

classmethod redirect(url, status_code=302, headers=None)[源代码]

返回重定向响应

classmethod text(content, status_code=200, headers=None)[源代码]

返回纯文本响应

class litefs.handlers.request.WSGIRequestHandler(app, environ)[源代码]

基类:BaseRequestHandler

WSGI 请求处理器,用于在 gunicorn、uWSGI 等 WSGI 服务器中运行

符合 PEP 3333 规范,处理 WSGI environ 并返回标准响应

property body
property charset
property config
property content_length
property content_type
property cookie
property data
property environ
property files
handler()[源代码]
property json
property method
property params
property path_info
property query_string
property referer
property request_method
property request_uri
property server_protocol
property session
property session_id
litefs.handlers.request.double_slash_sub(repl, string, count=0)

Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.

litefs.handlers.request.form_dict_match(string, pos=0, endpos=9223372036854775807)

Matches zero or more characters at the beginning of the string.

litefs.handlers.request.imap(func, iterable)[源代码]
litefs.handlers.request.is_bytes(s)[源代码]
litefs.handlers.request.parse_form(query_string)[源代码]

解析表单数据,支持缓存

参数:

query_string -- 查询字符串

返回:

解析后的表单数据字典

litefs.handlers.request.parse_header(line)[源代码]
litefs.handlers.request.startswith_dot_sub(repl, string, count=0)

Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.