To jest stare pytanie, ale wciąż zajmuje pierwsze miejsce w Google dla mnie, więc przypuszczam, że odpowiednia odpowiedź może być przydatna dla kogoś przy mnie.
Rozwiązanie okazuje się bardzo proste. w do_GET(), do_POST, itp, trzeba tylko dodać:
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 jest rzeczywista zawartość (jak w HTML, JavaScript lub innych zasobów HTML) i 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()
Zobacz tak sposobów poniżej – SoloPilot