From: Clark C . Evans <cce@cl...> - 2002-03-14 15:56:24
I know this is an older question, but what do I have to do
to send back gzippped content? Here is what I figure:
1. A function needs to be written "supportsCompression"
which examines the headers sent to determine if the
user agent accepts compressed content.
2. If the user agent supportsCompression, then before
the response is sent, it should be compressed and the
mime type should be updated respectively.
3. For files being downloaded, it should check to see
if the file is compressed; if so it returns file
directly if supportsCompression, otherwise the content
should be decompressed before it is sent.
Thoughts? Any pointers as to where/how the above could
From: Ian Bicking <ianb@co...> - 2002-03-14 18:33:23
On Thu, 2002-03-14 at 09:57, Clark C . Evans wrote:
> I know this is an older question, but what do I have to do
> to send back gzippped content? Here is what I figure:
> 1. A function needs to be written "supportsCompression"
> which examines the headers sent to determine if the
> user agent accepts compressed content.
> 2. If the user agent supportsCompression, then before
> the response is sent, it should be compressed and the
> mime type should be updated respectively.
The encoding shows whether the content is compressed -- the mime type
I posted some code before that was tested and worked -- it's simple, but
streaming and configuration weren't included. Here it is again:
import gzip # standard module
from cStringIO import StringIO
return # I don't believe we can double-encode...?
if not self.contentTypeCompressable(self.header('Content-type')):
compressed = StringIO()
gzipfile = gzip.GzipFile('', 'wb', 9, compressed) # We are writing to an empty-named, binary file, compression level 9
self._contents = [compressed.getvalue())
def contentTypeCompressable(self, type):
return type[:5] == 'text/'