用gzip或brotli压缩Quart应用程序中的响应。
quart-compress的Python项目详细描述
夸脱压缩
Quart Compress允许您使用gzip轻松压缩Quart应用程序的响应。在
首选的解决方案是让服务器(如Nginx)为您自动压缩静态文件。如果你没有这个选择夸脱压缩将解决你的问题。在
工作原理
Quart Compress同时添加压缩响应所需的各种头文件,并gzip处理响应数据。这使得提供gzip压缩的静态文件变得非常容易。在
在内部,每次发出请求时,扩展都会检查它是否与可压缩MIME类型之一匹配,并自动附加适当的头。在
安装
如果使用pip,则安装简单:
$ pip install quart-compress
或者,如果您想要最新的github版本:
^{pr2}$使用夸脱压缩
夸脱压缩非常简单。为了开始gzip处理Quart应用程序的资产,首先要让Quart Compress知道您的^{
fromquartimportQuartfromquart_compressimportCompressapp=Quart(__name__)Compress(app)
然而,在许多情况下,不能期望Quart实例在导入时准备就绪,一种常见的模式是,只有在处理了其他配置细节之后,才能从函数中返回Quart实例。在这些情况下,Quart Compress提供了一个简单的函数quart_compress.Compress.init_app
,它将应用程序作为一个参数。在
fromquartimportQuartfromquart_compressimportCompresscompress=Compress()defstart_app():app=Quart(__name__)compress.init_app(app)returnapp
就使用gzip自动压缩资产而言,只需将^{quart_compress.Compress
对象即可。在
选项
在Quart应用程序的设置中,可以提供以下设置来控制Quart Compress的行为。不需要任何设置。在
Option | Description | Default |
---|---|---|
^{ | Set the list of mimetypes to compress here. | ^{ ^{ ^{ ^{ ^{ ^{ ^{ |
^{ | Specifies the gzip compression level. | ^{ |
^{ | Specifies the minimum file size threshold for compressing files. | ^{ |
^{ | Specifies the cache key method for lookup/storage of response data. | ^{ |
^{ | Specified the backend for storing the cached response data. | ^{ |
^{ | Specifies if compression should be automatically registered. | ^{ |
- 项目
标签: