Skip to content

Instantly share code, notes, and snippets.

@martin-kokos
Last active January 11, 2018 09:42
Show Gist options
  • Save martin-kokos/5bcde6ef18dcc9232f3f5b55d791b285 to your computer and use it in GitHub Desktop.
Save martin-kokos/5bcde6ef18dcc9232f3f5b55d791b285 to your computer and use it in GitHub Desktop.
aiohttp decompress automatically based on Content-Encoding

Server

from aiohttp import web
import zlib

async def index(request):
    return web.Response(text=await request.text())

app = web.Application()
app.router.add_post('/', index)

web.run_app(app, host='127.0.0.1', port=8080)

Client

import aiohttp
import asyncio
import gzip

async def fetch(client):
    data = gzip.compress('Hello'.encode())
    async with client.post('http://localhost:8080/', data=data, headers={'Content-Encoding': 'gzip'}) as resp:
        assert resp.status == 200
        return await resp.text()

async def main(loop):
    async with aiohttp.ClientSession(loop=loop) as client:
        html = await fetch(client)
        print(html)

loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment