简体   繁体   English

如何使用Cherrypy在瓶子中启用gzip压缩

[英]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. ...这是Bottle项目的建议,最好由WSGI服务器Bottle运行在Gzip压缩上。 WSGI servers such as cherrypy provide a GzipFilter middleware that can be used to accomplish this. WSGI服务器(例如cherrypy)提供了一个GzipFilter中间件,可以用来完成此任务。

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? 如何告诉cherrypy使用gzip压缩?


I can get hold of the cherrypy server object like this, but I still can't work out how to enable gzip: 我可以像这样获得cherrypy服务器对象,但是我仍然不知道如何启用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. CherryPy具有Gzip工具,但仅适用于CherryPy本机应用程序。 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): 因此,您需要使用第三方Gzip WSGI中间件(wsgigzip仅用作示例,我不知道哪种中间件效果最好):

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. 或者更好的是,对服务器使用uWSGI ,除了许多其他出色功能之外,它还可以执行gzip

A stab in the dark (since I'm unfamiliar with CherryPy): drop this in where you have the "enable gzip here" comment. 在黑暗中刺伤(因为我不熟悉CherryPy):将其放在您具有“在此处启用gzip”注释的位置。

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

(Inspired by this .) 由此启发。)

Any luck? 运气好的话?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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