marrow.wsgi.egress.compression 1.1

A GZip compression egress filter for WSGI 2 applications on Python 2.6+ and 3.1+
marrow.wsgi.egress.compression is a GZip compression egress filter for WSGI 2 applications on Python 2.6+ and 3.1+.

What is GZip Compression Filtering?

Modern web browsers support compression of the HTML (or other) content that web servers return. Compressing the content allows the files to be downloaded faster and thus presented to the user faster, increasing the perceived speed of web browsing. This package implements an egress1 filter for compatible WSGI 2 servers, such as marrow.server.http.

Currently it is not possible to compress the output of an asynchronous body; if the WSGI environment variable wsgi.async is True, compression is disabled. Likewise, this filter does not compress MIME types other than text/* and application/*, excluding MIME types that include the word zip, such as applicaiton/x-gzip or application/zip — these files are already compressed, compressing them again will waste CPU cycles and likely increase the resulting content size.

As compression is an intensive process the improvement in perceived speed may be negligible for content that is difficult to compress. It can take longer to compress than the amount of time saved during over-the-wire transfer!

If you are using a front-end load balancer it would be better to utilize the compression features it has. For example, Apache’s mod_deflate and mod_gzip or Nginx’s HttpGzipModule.

1egress |ˈēˌgres| — noun — the action of going out of or leaving a place

Installation

Installing marrow.wsgi.egress.compression is easy, just execute the following in a terminal:

pip install marrow.wsgi.egress.compression

If you add marrow.wsgi.egress.compression to the install_requires argument of the call to setup() in your application’s setup.py file, the filter will be automatically installed and made available when your own application is installed.

Development Version

Development takes place on GitHub in the marrow.wsgi.egress.compression project. Issue tracking, documentation, and downloads are provided there.

Installing the current development version requires Git, a distributes source code management system. If you have Git, you can run the following to download and link the development version into your Python runtime:

git clone https://github.com/pulp/marrow.wsgi.egress.compression.git
(cd marrow.wsgi.egress.compression; make devel)

You can upgrade to the latest version at any time:

(cd marrow.wsgi.egress.compression; make update devel)

If you would like to make changes and contribute them back to the project, fork the GitHub project, make your changes, and submit a pull request. This process is beyond the scope of this documentation; for more information, see GitHub’s documentation.

Usage

Simply create a new instance of CompressionFilter and pass it to your WSGI server as an egress filter. Please refer to the documentation for your WSGI server of choice for instructions on how to do this. An example is provided below which utilizes the Marrow HTTP/1.1 server:[marrow-http].

In general this should be the last filter in the stack as the response body is no longer readable (it’s binary compressed GZip) once the filter has run.

The CompressionFilter class accepts a single argument, level, which defaults to
This is a number between 0 (no compression) and 9 (maximum compression) and is passed directly to gzip.GzipFile.

If the content returned by your WSGI application is incompressible (encrypted or already compressed) than additional compression may actually produce a larger response and negatively impact your application’s performance. To prevent this, the CompressionFilter examines the WSGI environment and will bypass compression if it finds a key in the environment named wsgi.compression that is False. You can disable compression by setting this. Other compression filters or middleware should likewise check for the presence of this value and bypass themselves if False.

Basic Example

import logging
from marrow.server.http import HTTPServer
from marrow.wsgi.egress.compression import CompressionFilter

def hello(request):
 return b'200 OK', [(b'Content-Type', b'text/plain'), (b'Content-Length', b'100')], [b'a' * 100]

if __name__ == '__main__':
 logging.basicConfig(level=logging.DEBUG)
 
 server = HTTPServer(None, 8080, application=hello, egress=[CompressionFilter(level=1)])
 server.start()


Acting as WSGI 2 Middleware

While this is technically an egress filter, processing only the trailing edge, outbound, of the request/response cycle, you can still use this filter as if it were full middleware. It is slightly less efficient to do this as there is additional stack overhead and method call nesting, however. If you must, you can follow this example:

import logging
from functools import partial
from marrow.server.http import HTTPServer
from marrow.wsgi.egress.compression import CompressionFilter

def hello(request):
 return b'200 OK', [(b'Content-Type', b'text/plain'), (b'Content-Length', b'100')], [b'a' * 100]

if __name__ == '__main__':
 logging.basicConfig(level=logging.DEBUG)
 
 egress = lambda app, filter, environ: filter(environ, *app(environ))
 
 server = HTTPServer(None, 8080, application=partial(egress, hello, CompressionFilter(level=6)))
 server.start()


The egress lambda function declared in this example is useful to adapt any egress filter for use as middleware.

last updated on:
November 23rd, 2010, 4:29 GMT
price:
FREE!
developed by:
Alice Bevan-McGregor
homepage:
github.com
license type:
MIT/X Consortium License 
category:
ROOT \ Internet \ HTTP (WWW)

FREE!

In a hurry? Add it to your Download Basket!

user rating

UNRATED
0.0/5
 

0/5

Rate it!

Add your review!

SUBMIT