Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gzip response in Flask/Tornado

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.

like image 873
TTT Avatar asked Aug 07 '12 13:08

TTT


2 Answers

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.

like image 118
Cole Maclean Avatar answered Sep 22 '22 16:09

Cole Maclean


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.

like image 37
TTT Avatar answered Sep 23 '22 16:09

TTT