Python, asyncio, and systemd

Ian Kelly ian.g.kelly at
Tue Jan 17 12:06:53 EST 2017

On Tue, Jan 17, 2017 at 9:57 AM, Chris Angelico <rosuav at> wrote:
> If I write a web server using asyncio (and the aiohttp package), I can
> spin up the server with:
> await loop.create_server(app.make_handler(), "", 8080)
> This works fine for a high port, but if I want to bind to port 80, I
> need to either start as root and then drop privileges, or get given
> the socket by someone else. With systemd, the latter is an option; you
> configure a service unit and a socket unit, and when the service gets
> started, it's given a bound listening socket as FD 3.
> Most of the work is pretty straight-forward, but I ran into one
> problem. The event loop's create_server() method calls a private
> _start_serving method, which means I can't (or rather, I shouldn't)
> just replicate create_server. This works, but it's naughty:
> sock = socket.socket(fileno=3)
> sock.setblocking(False)
> loop._start_serving(app.make_handler(), sock)
> What's the official way to say to asyncio "here's a listening socket,
> start managing it for me"?

I haven't tried this but create_server takes a "sock" keyword-argument.

More information about the Python-list mailing list