Search code examples
pythonhttp-compressionaiohttp

Enable gzip compression for static files in aiohttp


I'm trying to enable compression in aiohttp in static files too. I tried creating a custom response:

import aiohttp

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

and added it as factory to the static routes:

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

but it does not work.


Solution

  • Perhaps it doesn't work because currently sendfile approach doesn't switches to fallback if compression is set.