GZip Compression Egress Filter
GZip compression egress filter for WSGI2 applications.
© 2010, Alice Bevan-McGregor
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
Currently it is not possible to compress the output of an asynchronous body; if the WSGI environment variable
True, compression is disabled. Likewise, this filter does not compress MIME types other than
application/*, excluding MIME types that include the word
zip, such as
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!
1 egress |ˈēˌgres| — noun — the action of going out of or leaving a place
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.
2.1. Development Version
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.
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.
CompressionFilter class accepts a single argument,
level, which defaults to 6. This is a number between 0 (no compression) and 9 (maximum compression) and is passed directly to
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
3.1. 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()
3.2. 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()
egress lambda function declared in this example is useful to adapt any egress filter for use as middleware.
This project has been released under the MIT Open Source license.
4.1. The MIT License
Copyright © 2010 Alice Bevan-McGregor and contributors.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.