在 aiohttp 中为静态文件启用 gzip 压缩

Enable gzip compression for static files in aiohttp

我也在尝试在静态文件的 aiohttp 中启用压缩。我尝试创建自定义响应:

import aiohttp

class GzipResponse(aiohttp.web.StreamResponse):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.enable_compression()

并将其作为工厂添加到静态路由:

app.router.add_static("prefix", "path", response_factory=GzipResponse)

但它不起作用。

可能它不起作用,因为目前 sendfile 如果设置了压缩,方法不会切换到回退。