import aiohttp import asyncio async def main(): ws = await aiohttp.ws_connect('http://localhost:8080/ws') while True: msg = await ws.receive() if msg.tp == aiohttp.MsgType.binary: print(msg.data) elif msg.tp == aiohttp.MsgType.closed: break elif msg.tp == aiohttp.MsgType.error: break loop = asyncio.get_event_loop() loop.run_until_complete(main())