I think the problem is the following:
gzip.compress(b'test')
return a string with
header content THE END
in it.
This means when you read it the uncompressing will only return the b'test'. Try it yourself.
Two solutions depending on what you want to achieve:
- Create a multipart message. Every yield is a new document
compress
does this:def compress(data, compresslevel=9): """Compress data in one shot and return the compressed string. Optional argument is the compression level, in range of 0-9. """ buf = io.BytesIO() with GzipFile(fileobj=buf, mode='wb', compresslevel=compresslevel) as f: f.write(data) return buf.getvalue()
Do the something like this:
import gzip, io def application(environ, start_response): headers = [('Content-Type', 'text/html; charset=utf-8'),('Content-Encoding', 'gzip')] start_response('200 OK',headers) buf = io.BytesIO() with GzipFile(fileobj=buf, mode='wb') as f: f.write(b'test') f.write(b'test2') return buf