Snippet / decorator to handle gzip Content-Encoding

237 views
Skip to first unread message

bloofa

unread,
Mar 31, 2008, 8:54:45 PM3/31/08
to web.py
Hi there,

I've seen a few queries in the list archives about this. This is what
I use:

GZIP_PAT = re.compile('^Mozilla/[5,6,7,8,9]|.*MSIE [6,7,8,9].*')

def gzip_response(resp):
accepts = web.ctx.env.get('HTTP_ACCEPT_ENCODING',None)
if accepts and accepts.find('gzip') > -1:
browser = web.ctx.env.get('HTTP_USER_AGENT',None)
if browser and GZIP_PAT.match(browser): #ok to compress for
this browser
web.webapi.header('Content-Encoding','gzip')
zbuf = cStringIO.StringIO()
zfile = gzip.GzipFile(mode='wb', fileobj=zbuf,
compresslevel=9)
zfile.write(resp)
zfile.close()
data = zbuf.getvalue()
web.webapi.header('Content-Length',str(len(data)))
web.webapi.header('Vary','Accept-Encoding', unique=True)
#don't vary by user-agent, defeats caching
return data
return resp

class foo:
def GET(self,whatever):
...
print gzip_response( render.foo( whatever ))

Gary Bernhardt

unread,
Mar 31, 2008, 9:40:55 PM3/31/08
to we...@googlegroups.com
On 3/31/08, bloofa <blo...@gmail.com> wrote:
>
> Hi there,
>
> I've seen a few queries in the list archives about this. This is what
> I use:

Paste also has middleware for this, if you'd rather not build your own.

What prompted you to include the user-agent regex? Did you run into
problems with some browsers?

> GZIP_PAT = re.compile('^Mozilla/[5,6,7,8,9]|.*MSIE [6,7,8,9].*')
>
> def gzip_response(resp):
> accepts = web.ctx.env.get('HTTP_ACCEPT_ENCODING',None)
> if accepts and accepts.find('gzip') > -1:
> browser = web.ctx.env.get('HTTP_USER_AGENT',None)
> if browser and GZIP_PAT.match(browser): #ok to compress for
> this browser
> web.webapi.header('Content-Encoding','gzip')
> zbuf = cStringIO.StringIO()
> zfile = gzip.GzipFile(mode='wb', fileobj=zbuf,
> compresslevel=9)
> zfile.write(resp)
> zfile.close()
> data = zbuf.getvalue()
> web.webapi.header('Content-Length',str(len(data)))
> web.webapi.header('Vary','Accept-Encoding', unique=True)
> #don't vary by user-agent, defeats caching
> return data
> return resp
>
> class foo:
> def GET(self,whatever):
> ...
> print gzip_response( render.foo( whatever ))
> >
>


--
Gary
http://blog.extracheese.org

bloofa

unread,
Mar 31, 2008, 11:38:31 PM3/31/08
to web.py
This is essentially the recommendation of Steve Souders in his
O'Reilly book "High Performance Web Sites", page 34. There are a few
browsers that mis-report whether they can handle gzip; this is one way
of handling some of them.

I didn't know about Paste, will have a look, thanks.

John

Paul Jobs

unread,
Apr 1, 2008, 12:12:06 AM4/1/08
to we...@googlegroups.com
Try beaker or flup for gzip
Reply all
Reply to author
Forward
0 new messages