Home

Tags

Unix socket in asyncio

2018-05-01 python asyncio

Server

from aiohttp import web

async def handle(request):
    return web.Response(body=b'OK', status=200)

app = web.Application()
app.router.add_get('/test', handle)
web.run_app(app, path='/tmp/server.sock')

Client

import asyncio
import aiohttp

async def request():
    conn = aiohttp.UnixConnector(path='/tmp/server.sock')
    try:
        async with aiohttp.request('GET', f'http://localhost/test', connector=conn) as resp:
            assert resp.status == 200
            print(await resp.text())
    finally:
        conn.close()

loop = asyncio.get_event_loop()
loop.run_until_complete(request())
loop.close()