使用 Pyramid 对所有 HTTP 流量进行压缩

发布于 2024-11-18 19:02:14 字数 190 浏览 3 评论 0原文

我正在创建基于金字塔框架的移动服务。因为它是移动的,所以减少带宽使用的一切都是有利的。我正在考虑压缩所有流量,甚至是动态 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜灵血窟げ 2024-11-25 19:02:14

首先我要强调的是,你应该在 Web 服务器级别(nginx 或 apache)上执行此操作。造成这种情况的原因有几个:

  1. 性能 - 如果您在 Python 中执行此操作,您正在使用的线程之一可能会处理进行 CPU 密集型压缩的请求。这比让优化的 Web 服务器处理它的效率要低得多。

  2. 阻止 - 大多数 GZip 中间件都会阻止您的响应,缓冲正文,以便它可以压缩整个响应。如果您尝试将任何响应流式传输回客户端,这将是一个巨大问题,因为它会陷入中间件中。这实际上违反了 PEP333(WSGI 规范)。

考虑到所有这些,在 Python 中执行此操作可能是有意义的,至少在开发过程中出于调试目的。

由于您已经在使用 Pyramid,因此您已经安装了 Paste。因此,您可以简单地将 paste.gzipper.GzipMiddleware 添加到应用程序的管道中,如下所示:

[filter:gzip]
use = egg:Paste#gzip
compress_level = 6

[pipeline:main]
pipeline =
    gzip
    app

显然,如果您不想更改默认的 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:

  1. 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.

  2. 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:

[filter:gzip]
use = egg:Paste#gzip
compress_level = 6

[pipeline:main]
pipeline =
    gzip
    app

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).

吃颗糖壮壮胆 2024-11-25 19:02:14

您仍然可以使用 Apache 获取每个请求的压缩统计信息。我创建了一个 deflate.log,如下所示:

DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio
LogFormat '%a %v %{outstream}n/%{instream}n (%{ratio}n%%) "%r"' deflate
CustomLog /var/log/httpd/deflate.log deflate

现在我得到如下日志条目:

ip domain -/- (-%) "GET /wp-content/uploads/2010/03/favicon.ico HTTP/1.1"
ip domain 10995/52406 (20%) "GET /2006/07/19/ HTTP/1.0"
ip domain 1873/7891 (23%) "POST /registration/regForm HTTP/1.1"

我可以随心所欲地进行分析。

You can still get per-request compression statistics with Apache. I created a deflate.log like so:

DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio
LogFormat '%a %v %{outstream}n/%{instream}n (%{ratio}n%%) "%r"' deflate
CustomLog /var/log/httpd/deflate.log deflate

Now I get log entries like:

ip domain -/- (-%) "GET /wp-content/uploads/2010/03/favicon.ico HTTP/1.1"
ip domain 10995/52406 (20%) "GET /2006/07/19/ HTTP/1.0"
ip domain 1873/7891 (23%) "POST /registration/regForm HTTP/1.1"

that I can analyze to my heart's content.

我的黑色迷你裙 2024-11-25 19:02:14

以下是 WSGI 级别的几个选项:

Here are a couple of options at the WSGI level:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文