Esta es una vieja pregunta, pero todavía ocupa el n. ° 1 en Google para mí, por lo que supongo que una respuesta adecuada podría ser útil para alguien a mi lado.
La solución resulta ser muy simple. en el do_GET(), do_POST, etc, sólo tiene que añadir lo siguiente:
content = self.gzipencode(strcontent)
...your other headers, etc...
self.send_header("Content-length", str(len(str(content))))
self.send_header("Content-Encoding", "gzip")
self.end_headers()
self.wfile.write(content)
self.wfile.flush()
strcontent ser su contenido real (como en HTML, JavaScript u otros recursos HTML) y la gzipencode:
def gzipencode(self, content):
import StringIO
import gzip
out = StringIO.StringIO()
f = gzip.GzipFile(fileobj=out, mode='w', compresslevel=5)
f.write(content)
f.close()
return out.getvalue()
Ver yes maneras abajo – SoloPilot