编写wsgi应用程序和中间件的更好方法
wsgi_lite的Python项目详细描述
如果编写correctwsgi中间件这么简单,不是很好吗?
>>> from wsgi_lite import lite, lighten >>> def latinator(app): ... ... # Make sure that `app` can be invoked via the Lite protocol, even ... # if it's a standard WSGI 1 app: ... app = lighten(app) ... ... @lite ... def middleware(environ): ... status, headers, body = app(environ) ... for name, value in headers: ... if name.lower()=='content-type' and value=='text/plain': ... break ... else: ... # Not text/plain, pass the request through unchanged ... return status, headers, body ... ... # Strip content-length if present, else it'll be wrong ... headers = [ ... (name, value) for name, value in headers ... if name.lower() != 'content-length' ... ] ... return status, headers, (piglatin(data) for data in body) ... ... return middleware
仅使用两个装饰器,wsgi-lite就可以创建正确且兼容的 中间件和应用程序,无需担心start_response, write和close调用。对于那些same两个装饰器,它也 允许您管理在请求结束时释放的资源,以及 自动将关键字参数传递给应用程序或中间件 从wsgi环境(如wsgi服务器扩展或 中间件提供的参数,如请求或会话对象)。
有关详细信息,请查看project’s home page on BitBucket,然后向下滚动到 目录。
wsgi lite目前仅适用于python 2.x(测试版本为2.3到2.7) 但是源代码应该可以移植到3.x中,因为它的魔力仅限于 检查函数参数名,并使用 new.function()。