[英]How do I enable gzip compression in bottle with cherrypy
瓶子文档说:
...这是Bottle项目的建议,最好由WSGI服务器Bottle运行在Gzip压缩上。 WSGI服务器(例如cherrypy)提供了一个
GzipFilter
中间件,可以用来完成此任务。
目前,我的瓶子服务器正在运行:
app.run(host='...', port=8080, server='cherrypy')
如何告诉cherrypy使用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具有Gzip工具,但仅适用于CherryPy本机应用程序。 因此,您需要使用第三方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()
在黑暗中刺伤(因为我不熟悉CherryPy):将其放在您具有“在此处启用gzip”注释的位置。
cherrypy.config.update({'tools.gzip.on': True})
( 由此启发。)
运气好的话?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.