使用 Pyramid 对所有 HTTP 流量进行压缩
我正在创建基于金字塔框架的移动服务。因为它是移动的,所以减少带宽使用的一切都是有利的。我正在考虑压缩所有流量,甚至是动态 HTML 页面。
Pyramid 框架为此提供了什么样的钩子?或者是否有用于该任务的 WSGI 中间件?我仍然想在 Python 级别上执行此操作,而不是 Nginx/Apache,这样我可以更好地统计 gzip 带来的好处。
I am creating a mobile service based on Pyramid framework. Because it's mobile everything to reduce bandwidth usage is plus. I am considering gzipping all the traffic, even dynamic HTML pages.
What kind of hooks Pyramid framework provides for this? Or is there WSGI middleware for the task? I'd like to do this still on Python level, not Nginx/Apache, so I can better statistics how much gzip brings benefirts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先我要强调的是,你应该在 Web 服务器级别(nginx 或 apache)上执行此操作。造成这种情况的原因有几个:
性能 - 如果您在 Python 中执行此操作,您正在使用的线程之一可能会处理进行 CPU 密集型压缩的请求。这比让优化的 Web 服务器处理它的效率要低得多。
阻止 - 大多数 GZip 中间件都会阻止您的响应,缓冲正文,以便它可以压缩整个响应。如果您尝试将任何响应流式传输回客户端,这将是一个巨大问题,因为它会陷入中间件中。这实际上违反了 PEP333(WSGI 规范)。
考虑到所有这些,在 Python 中执行此操作可能是有意义的,至少在开发过程中出于调试目的。
由于您已经在使用 Pyramid,因此您已经安装了 Paste。因此,您可以简单地将
paste.gzipper.GzipMiddleware
添加到应用程序的管道中,如下所示:显然,如果您不想更改默认的 6 压缩级别,您可以简单地添加
Egg:Paste#gzip
到管道,而不是配置过滤器并为其指定自定义名称 (gzip
)。First of all I should stress that you should do this on the web server level (nginx or apache). There are several reasons for this:
Performance - If you do this in Python you are using one of your threads that could be handling requests to do cpu-intensive compression. This is way less efficient than allowing your optimized web server to handle it.
Blocking - Most GZip middleware will block your responses, buffering the body so that it can compress the entire response. This is a huge problem if you are attempting to stream any response back to the client because it will get caught in middleware. This is actually a violation of PEP333, the WSGI specification.
With all of this in mind, it might make sense to do it in Python at least for debugging purposes during development.
Since you're already using Pyramid then you have Paste installed. Thus you can simply add the
paste.gzipper.GzipMiddleware
to your application's pipeline like so:Obviously if you don't want to change the compression level from the default of 6 you can simply add the
egg:Paste#gzip
to the pipeline in place of configuring the filter and giving it a custom name (gzip
).您仍然可以使用 Apache 获取每个请求的压缩统计信息。我创建了一个 deflate.log,如下所示:
现在我得到如下日志条目:
我可以随心所欲地进行分析。
You can still get per-request compression statistics with Apache. I created a deflate.log like so:
Now I get log entries like:
that I can analyze to my heart's content.
以下是 WSGI 级别的几个选项:
Here are a couple of options at the WSGI level: