簡體   English   中英

如何使用Cherrypy在瓶子中啟用gzip壓縮

[英]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()

或者更好的是,對服務器使用uWSGI ,除了許多其他出色功能之外,它還可以執行gzip

在黑暗中刺傷(因為我不熟悉CherryPy):將其放在您具有“在此處啟用gzip”注釋的位置。

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

由此啟發。)

運氣好的話?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM