15

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?

0

6 Answers 6

16

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()
Sign up to request clarification or add additional context in comments.

1 Comment

Of course, the above were straight copy-pastes of that code. But then again, the above snippets say it all and the complete functioning code also serves as a JSON bridge which would kinda blur the purpouse of this question. Anyway, drop me a mail if you need more info or the code.
9

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

Comments

5

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

Comments

3

This was a feature request but rejected due to wanting to keep the simple http server simple: https://bugs.python.org/issue30576

The issue author eventually released a standalone version for Python 3: https://github.com/PierreQuentel/httpcompressionserver

2 Comments

Just a heads up for others. httpcompressionserver did not automatically bind the address the same way as "http.server". For me it crashed. I had to start it with: python3 -m httpcompressionserver --bind 0.0.0.0 See: github.com/PierreQuentel/httpcompressionserver/issues/2
That should've been fixed on pypi. Contact the author if it hasn't.
2

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)

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.