WSGI 支持实现总结

✅ 已完成的功能

1. 核心 WSGI 实现

  • ✅ 实现了符合 PEP 3333 规范的 wsgi() 方法

  • ✅ 创建了完整的 WSGIRequestHandler

  • ✅ 支持 WSGI environ 变量标准化

  • ✅ 实现了 multipart/form-data 解析

  • ✅ 支持请求体读取和表单解析

2. 兼容性修复

  • ✅ 修复了 Python 3.14 中 cgi 模块被移除的问题

  • ✅ 使用 email.message.Message 替代 cgi.parse_header

  • ✅ 更新 requirements.txt 使用 >= 版本约束

  • ✅ 修复了重复的 Session 类定义

3. 部署支持

  • ✅ 支持 Gunicorn 部署

  • ✅ 支持 uWSGI 部署

  • ✅ 支持 Waitress 部署(Windows 推荐)

4. 文档和示例

⚠️ Python 版本兼容性

当前状态

Python 版本

内置服务器

WSGI 部署

推荐度

2.6-2.7

✅ 完全支持

✅ 完全支持

⭐⭐

3.4-3.12

✅ 完全支持

✅ 完全支持

⭐⭐⭐⭐⭐⭐

3.13

⚠️ 可能有问题

✅ 完全支持

⭐⭐

3.14+

❌ greenlet 不兼容

✅ 完全支持

说明

  1. greenlet 依赖问题

    • greenlet 3.3.2 在 Python 3.14 + Windows 上存在 DLL 加载问题

    • 这是 greenlet 本身的兼容性问题,不是 Litefs 的问题

    • greenlet 仅用于内置的 epoll 服务器

  2. WSGI 部署不受影响

    • WSGI 接口实现不依赖 greenlet

    • 可以在任何 Python 版本中使用 WSGI 服务器部署

    • Gunicorn、uWSGI、Waitress 等服务器都有自己的 worker 实现

  3. 推荐方案

    • 生产环境: 使用 Python 3.9-3.12

    • 开发环境: 可以使用任何 Python 版本

    • Python 3.14 用户: 使用 WSGI 服务器部署,不使用内置服务器

🚀 使用示例

WSGI 部署(推荐)

创建 wsgi_example.py:

import litefs
app = litefs.Litefs(webroot='./site')
application = app.wsgi()

使用 Gunicorn:

gunicorn -w 4 -b :8000 wsgi_example:application

使用 uWSGI:

uwsgi --http :8000 --wsgi-file wsgi_example.py

使用 Waitress (Windows):

waitress-serve --port=8000 wsgi_example:application

内置服务器(需要 Python 3.4-3.12)

import litefs
litefs.test_server()

或命令行:

litefs --host localhost --port 9090 --webroot ./site

📋 核心特性

  1. PEP 3333 完全兼容

    • 正确的 application callable

    • 标准的 environ 处理

    • 正确的 start_response 调用

  2. 完整的请求处理

    • GET/POST 请求支持

    • 文件上传支持

    • Session 管理

    • Cookie 处理

  3. 性能优化

    • 多级缓存系统

    • 静态文件压缩

    • HTTP 缓存控制(ETag/Last-Modified)

  4. 生产就绪

    • 错误处理

    • 日志记录

    • 调试模式支持

🔧 技术实现

WSGI 接口设计

class Litefs:
    def wsgi(self):
        def application(environ, start_response):
            try:
                request_handler = WSGIRequestHandler(self, environ)
                status, headers, content = request_handler.handler()
                start_response(status, headers)
                return [content.encode('utf-8')]
            except Exception as e:
                log_error(self.logger, str(e))
                status = '500 Internal Server Error'
                headers = [('Content-Type', 'text/plain; charset=utf-8')]
                start_response(status, headers)
                return [b'500 Internal Server Error']
        
        return application

WSGIRequestHandler 功能

  • _normalize_environ(): 标准化 WSGI environ 变量

  • _parse_multipart(): 解析 multipart/form-data

  • _get_session(): Session 管理

  • handler(): 请求路由和处理

  • _load_script(): 脚本加载

  • _load_static_file(): 静态文件处理

📝 测试

测试脚本

  • test_wsgi.py: 完整的 WSGI 规范测试

  • test_wsgi_simple.py: 简化的导入测试

  • test_wsgi_no_greenlet.py: 不依赖 greenlet 的测试

  • test_basic.py: 基本功能测试

运行测试

# 基本功能测试
python test_basic.py

# WSGI 接口测试(需要 greenlet)
python test_wsgi_simple.py

🎯 后续优化建议

  1. Python 3.14+ 支持

    • 等待 greenlet 官方支持 Python 3.14

    • 或考虑使用其他协程库(如 asyncio)

  2. 性能优化

    • 添加异步 worker 支持

    • 优化缓存策略

    • 添加连接池

  3. 功能增强

    • 添加更多 HTTP 方法支持

    • 改进错误处理

    • 添加监控指标

  4. 文档完善

    • 添加 API 文档

    • 添加更多示例

    • 添加最佳实践指南

📚 相关文档