Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Tree: 66a5df7c51
Fetching contributors…

Cannot retrieve contributors at this time

33 lines (25 sloc) 1.09 kB
import gzip
import StringIO
from flask import request
class Gzip(object):
def __init__(self, app, compress_level=6): = app
self.compress_level = compress_level
def after_request(self, response):
accept_encoding = request.headers.get('Accept-Encoding', '')
if not accept_encoding:
return response
encodings = accept_encoding.split(',')
if 'gzip' not in encodings:
return response
if (200 > response.status_code >= 300) or len( < 500 or 'Content-Encoding' in response.headers:
return response
gzip_buffer = StringIO.StringIO()
gzip_file = gzip.GzipFile(mode='wb', compresslevel=self.compress_level, fileobj=gzip_buffer)
gzip_file.close() = gzip_buffer.getvalue()
response.headers['Content-Encoding'] = 'gzip'
response.headers['Content-Length'] = len(
return response
Jump to Line
Something went wrong with that request. Please try again.