7

I would like to add a REST API to my application. I already have some (non-REST) UNIX socket listeners using Python's asyncio which I would like to keep. Most frameworks I have found for implementing REST APIs seem to require starting their own event loop (which conflicts with asyncio's event loop).

What is the best approach/library for combining REST/UNIX socket listeners without having to roll my own implementation from scratch?

Thanks in advance!!

1 Answer 1

10

OK, to answer my question, the above works quite nicely using aiohttp. For future reference, here is a minimal example adopted from the aiohttp documentation:

import asyncio
import code
from aiohttp import web


async def handle(request):
    name = request.match_info.get('name', "Anonymous")
    text = "Hello, " + name
    return web.Response(text=text)

app = web.Application()
app.router.add_get('/', handle)
app.router.add_get('/{name}', handle)

loop = asyncio.get_event_loop()
handler = app.make_handler()
f = loop.create_server(handler, '0.0.0.0', 8080)
srv = loop.run_until_complete(f)

loop.run_forever()
code.interact(local=locals())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.