简体   繁体   中英

How do I enable gzip compression in bottle with cherrypy

The bottle docs say:

... it is the recommendation of the Bottle project that Gzip compression is best handled by the WSGI server Bottle runs on top of. WSGI servers such as cherrypy provide a GzipFilter middleware that can be used to accomplish this.

At present, I'm running my bottle server with:

app.run(host='...', port=8080, server='cherrypy')

How can I tell cherrypy to use gzip compression?


I can get hold of the cherrypy server object like this, but I still can't work out how to enable gzip:

class CherryPyGzip(ServerAdapter):
  def run(self, handler): 
    from cherrypy import wsgiserver
    server = wsgiserver.CherryPyWSGIServer((self.host, self.port), handler)

    # enable gzip here somehow?

    try:
      server.start()
    finally:
      server.stop()

app.run(host='...', port=8080, server=CherryPyGzip)

CherryPy has Gzip tool, but it only works with CherryPy native apps. So you need to use 3rd party Gzip WSGI middleware (wsgigzip is used only as an example, I have no idea which middleware works best):

import cherrypy
import wsgigzip


application = wsgigzip.GzipMiddleware(bottle.default_app())

cherrypy.config.update({'server.socket_host': "0.0.0.0",
                        'server.socket_port': 8080})
cherrypy.tree.graft(application, "/")
cherrypy.engine.start()
cherrypy.engine.block()

Or better yet, use uWSGI for the server, it can do gzip in addition to many other great features.

A stab in the dark (since I'm unfamiliar with CherryPy): drop this in where you have the "enable gzip here" comment.

cherrypy.config.update({'tools.gzip.on': True})

(Inspired by this .)

Any luck?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM