How to use Content-Encoding: gzip with Python Simp

2019-06-20 08:37发布

I'm using python -m SimpleHTTPServer to serve up a directory for local testing in a web browser. Some of the content includes large data files. I would like to be able to gzip them and have SimpleHTTPServer serve them with Content-Encoding: gzip.

Is there an easy way to do this?

5条回答
仙女界的扛把子
2楼-- · 2019-06-20 08:45

This is an old question, but it still ranks #1 in Google for me, so I suppose a proper answer might be of use to someone beside me.

The solution turns out to be very simple. in the do_GET(), do_POST, etc, you only need to add the following:

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 being your actual content (as in HTML, javascript or other HTML resources) and the 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()
查看更多
干净又极端
3楼-- · 2019-06-20 08:45

As so many others, I've been using python -m SimpleHTTPServer for local testing as well. This is still the top result on google and while https://github.com/ksmith97/GzipSimpleHTTPServer is a nice solution, it enforces gzip even if not requested and there's no flag to enable/disable it.

I decided to write a tiny cli tool that supports this. It's go, so the regular install procedure is simply:

go get github.com/rhardih/serve

If you already have $GOPATH added to $PATH, that's all you need. Now you have serve as a command.

https://github.com/rhardih/serve

查看更多
家丑人穷心不美
4楼-- · 2019-06-20 08:50

Since this was the top google result I figured I would post my simple modification to the script that got gzip to work.

https://github.com/ksmith97/GzipSimpleHTTPServer

查看更多
smile是对你的礼貌
5楼-- · 2019-06-20 08:57

Building on @velis answer above, here is how I do it. gZipping small data is not worth the time and can increase its size. Tested with Dalvik client.

def do_GET(self):
    ... get content
    self.send_response(returnCode)       # 200, 401, etc
    ...your other headers, etc...
    if len(content) > 100:                       # don't bother compressing small data
        if 'accept-encoding' in self.headers:    # case insensitive
            if 'gzip' in self.headers['accept-encoding']:
                content = gzipencode(content)    # gzipencode defined above in @velis answer
                self.send_header('content-encoding', 'gzip')
    self.send_header('content-length', len(content))
    self.end_headers()          # send a blank line
    self.wfile.write(content)
查看更多
迷人小祖宗
6楼-- · 2019-06-20 09:00

From looking at SimpleHTTPServer's documentation, there is no way. However, I recommend lighttpd with the mod_compress module.

查看更多
登录 后发表回答