Gzip response in Flask/Tornado


Question

I have a simple Flask app running. To serve I use Tornado. The code that starts the server looks like this:

# Run the app in server mode
http_server = HTTPServer(WSGIContainer(app))
http_server.listen(port)
IOLoop.instance().start()

Where app is the Flask WSGI app object (app = Flask(__name__)).

Now the server responds every request with the entire JSON answer, is there a (simple) way of returning Gzipped data? On the Tornado site I found http://www.tornadoweb.org/documentation/releases/v2.1.0.html?highlight=gzip, so it must be possible with Tornado, but a Flask solution is also great.

1
4
9/5/2013 4:43:42 PM

Accepted Answer

It just appears weird to set up an nginx only to have gzip compression.

Now I use this http://code.google.com/p/ibkon-wsgi-gzip-middleware/, it's good.

4
8/9/2012 9:54:39 AM

The simplest way is to use Nginx, as Nikolay suggested. It won't add much overhead.

With tornado.web.Application, you can just pass compress_response=True when initializing the app. Since you're using a Flask, that won't work. You could look at the Tornado source and see what it's doing, but that won't be simple.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon