<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:courier, monaco, monospace, sans-serif;font-size:12pt"><DIV style="FONT-SIZE: 12pt; FONT-FAMILY: courier, monaco, monospace, sans-serif">Thanks.</DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: courier, monaco, monospace, sans-serif"> </DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: courier, monaco, monospace, sans-serif">So it seems I have to write my own code, there in nothing like I can set some flag.</DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: courier, monaco, monospace, sans-serif"> </DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: courier, monaco, monospace, sans-serif">So if that is the case shouldn't I just be gzipping and unzipping data write and read to request object.</DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: courier, monaco, monospace, sans-serif"> </DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: courier, monaco, monospace, sans-serif">rgds</DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: courier, monaco, monospace, sans-serif">anurag<BR><BR></DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">----- Original Message ----<BR>From: David Bolen <db3l.net@gmail.com><BR>To: twisted-python@twistedmatrix.com<BR>Sent: Friday, October 19, 2007 1:02:21 AM<BR>Subject: [Twisted-Python] Re: Content Encoding : gzip ?<BR><BR>
<DIV>anurag uniyal <anuraguniyal@yahoo.com> writes:<BR><BR>> Is it possible to send and recieve compressed content using twisted.web?<BR>><BR>> I have set Accept-encoding to gzip but it doesn't make any difference.<BR>> Will I have to cater for this myself?<BR><BR>Yes, in twisted.web. In twisted.web2, there's a filter that supports it.<BR><BR>Here's a small wrapper class I wrote recently that I've used to handle<BR>this in twisted.web, with the goal of minimal changes to existing<BR>resource code. I use it by noticing, in the original request handler,<BR>the ability to handle gzip encodings, and then wrap the original<BR>request in this class (since there's no built-in filtering of the<BR>output stream in twisted.web).<BR><BR>It's worked fine in my tests to date, and in limited production use,<BR>but to be honest it hasn't seen large scale, long term use because my<BR>original use case only yielded ~15% compression
(multimedia files) and<BR>using this negates the ability to know the full length in advance,<BR>thus clients can't give projected download times. (My files are large<BR>enough that compressing twice is high overhead, and it wouldn't play<BR>well with the wrapper approach anyway).<BR><BR>In any event, hopefully it'll be a useful starting point for you.<BR><BR>Here's the wrapper:<BR><BR> - - - - - - - - - - - - - - - - - - - - - - - - -<BR><BR>import struct<BR>import zlib<BR><BR>class GzipRequest(object):<BR> """Wrapper for a request that applies a gzip content encoding"""<BR><BR> def __init__(self, request, compressLevel=6):<BR> self.request = request<BR> self.request.setHeader('Content-Encoding',
'gzip')<BR> # Borrowed from twisted.web2 gzip filter<BR> self.compress = zlib.compressobj(compressLevel, zlib.DEFLATED,<BR> -zlib.MAX_WBITS, zlib.DEF_MEM_LEVEL,0)<BR><BR> def __getattr__(self, attr):<BR> if 'request' in self.__dict__:<BR> return getattr(self.request, attr)<BR> else:<BR> raise AttributeError, attr<BR><BR> def __setattr__(self, attr,
value):<BR> if 'request' in self.__dict__:<BR> return setattr(self.request, attr, value)<BR> else:<BR> self.__dict__[attr] = value<BR><BR> def write(self, data):<BR> if not self.request.startedWriting:<BR> self.crc = zlib.crc32('')<BR> self.size = self.csize = 0<BR> # XXX: Zap any length for now since we don't know final size<BR> if 'content-length' in
self.request.headers:<BR> del self.request.headers['content-length']<BR> # Borrow header information from twisted.web2 gzip filter<BR> self.request.write('\037\213\010\000' '\0\0\0\0' '\002\377')<BR><BR> self.crc = zlib.crc32(data, self.crc)<BR> self.size += len(data)<BR> cdata = self.compress.compress(data)<BR> self.csize += len(cdata)<BR> if
cdata:<BR> self.request.write(cdata)<BR> elif self.request.producer:<BR> # Simulate another pull even though it hasn't really made it<BR> # out to the consumer yet.<BR> self.request.producer.resumeProducing()<BR><BR> def finish(self):<BR> remain = self.compress.flush()<BR> self.csize += len(remain)<BR> if
remain:<BR> self.request.write(remain)<BR> self.request.write(struct.pack('<LL',<BR> self.crc & 0xFFFFFFFFL,<BR> self.size & 0xFFFFFFFFL))<BR> self.request.finish()<BR><BR> - - - - - - - - - - - - - - - - - - - - - - - - -<BR><BR>and here's a sample of using
it. This code is from one of my Resource<BR>objects - if I were to use it more generally in my site I'd extract that<BR>into a mix-in class of some sort - currently there was only one specific<BR>resource (the multimedia files) I wanted to support it for:<BR><BR> def render_GET(self, request):<BR><BR> # (... argument validation ...)<BR> <BR> accept_encoding = request.getHeader('accept-encoding')<BR> if accept_encoding:<BR> encodings = accept_encoding.split(',')<BR> for encoding in encodings:<BR> name =
encoding.split(';')[0].strip()<BR> if name == 'gzip':<BR> request = GzipRequest(request)<BR> break<BR><BR> # At this point, use 'request' as normal<BR><BR><BR>-- David<BR><BR><BR>_______________________________________________<BR>Twisted-Python mailing list<BR>Twisted-Python@twistedmatrix.com<BR><A href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target=_blank>http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</A></DIV></DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: courier, monaco, monospace, sans-serif"><BR></DIV></div><br>__________________________________________________<br>Do You Yahoo!?<br>Tired of spam? Yahoo! Mail has the best spam protection around <br>http://mail.yahoo.com </body></html>