使用Pyramid Gzipping所有HTTP流量 [英] Gzipping all HTTP traffic with Pyramid

查看:117
本文介绍了使用Pyramid Gzipping所有HTTP流量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个基于Pyramid框架的移动服务。因为它是移动的,所以减少带宽使用的一切都是加分。我正在考虑对所有流量进行压缩,甚至是动态HTML页面。

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.

Pyramid框架为此提供了什么样的钩子?或者是否有WSGI中间件用于任务?我想在Python级别上做这个,而不是Nginx / Apache,所以我可以更好地统计gzip带来多少好处。

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服务器级别(nginx或apache)上执行此操作。这有几个原因:

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. 性能 - 如果你在Python中执行此操作,那么你使用的是一个可能的线程处理执行cpu密集压缩的请求。这比允许优化的Web服务器处理它的效率低。

  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.

阻止 - 大多数GZip中间件会阻止你的响应,缓冲身体以便它可以压缩整个响应。如果您尝试将任何响应流回客户端,这是一个巨大的问题,因为它会被中间件捕获。这实际上违反了WSGI规范PEP333。

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.

考虑到所有这些,它可能有意义在Python中至少为了调试目的而在Python中进行。

With all of this in mind, it might make sense to do it in Python at least for debugging purposes during development.

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

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

显然,如果你不想改变压缩等级默认值为6,您只需将 egg:Paste#gzip 添加到管道中,而不是配置过滤器并为其指定自定义名称( gzip )。

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

这篇关于使用Pyramid Gzipping所有HTTP流量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆