2010/11/16 KenCorey <[email protected]>: > On Nov 15, 11:56 pm, Hraban Luyat <[email protected]> wrote: >> It's been a while so my knowledge of all these things is a little >> rusty, but iirc mod_compress operates on static files. web.py is an > [...] >> >> In related news, what you probably want is a caching proxy. They can >> do everything you want, and more. Try Squid; the learning curve is > > Ah, I see. > > I don't think that I want to use a proxy, because the knowledge of > whether something should be compressed or not is in my application, > and I don't trust myself to adjust the squid settings properly...which > got me to thinking "Why not compress the output myself?" So, at the > bottom of the GET method on my page classes I try to compress the > output myself, in the code below. > > Where I would normally return from a GET with: > return > render.generic(bodycontent,menu,header,footer,loginblock,stylesheet,bottomblock) > > I now return with: > return > compreturn(render.generic(bodycontent,menu,header,footer,loginblock,stylesheet,bottomblock)) > > And here's compreturn: > def compreturn(cnt): > if web.ctx.environ.has_key('HTTP_ACCEPT_ENCODING') and > web.ctx.environ['HTTP_ACCEPT_ENCODING'].find('gzip')>-1: > scnt = str(cnt) > zcnt = cStringIO.StringIO() > zfile = gzip.GzipFile('','wb',9,zcnt) > zfile.write(scnt) > zfile.close() > zsize = len(zcnt.getvalue()) > if len(scnt)>zsize: > web.header("Content-Encoding","gzip") > web.header("Content-Length",str(zsize)) > return zcnt.getvalue() > else: > return cnt > else: > return cnt > > Can anyone see any pitfalls here? Any ways to do this more > efficiently?
I've written a WSGI middleware to gzip the response. https://github.com/openlibrary/openlibrary/blob/master/openlibrary/plugins/upstream/utils.py#L573 Feel free to adopt it to you needs. Anand -- You received this message because you are subscribed to the Google Groups "web.py" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/webpy?hl=en.
