How open a Unix domain socket with asyncio?

Hi,

in the documentation of asyncio streams There is explaination on how open a connection/stream for TCP. but nothing for Unix domain socket :expressionless:

I’ve tried

import asyncio

async def main():
    reader, writer = await asyncio.open_connection(
        family=AddressFamily.AF_UNIX,
        proto=0,
        local_addr='/tmp/MySocket')

main()

but this give me:

RuntimeWarning: coroutine ‘main’ was never awaited
main()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

Any ideas or documentation that referrer to Unix domain socket with asyncio streams ?

Hello,

You should run your main function with asyncio.run(main()).

Here you only create a couroutine object that you never run.

1 Like

Thanks @entwanne

now, I get

name ‘AddressFamily’ is not defined

When you get that sort of error it’s likely that you forgot an import.

Where did you find AF_UNIX in the docs? It should it clear what module its from so you can import it.

AF_UNIX is used with the "classical "

from socket import socket, AF_UNIX, SOCK_STREAM, timeout
with socket(AF_UNIX, SOCK_STREAM)

to open a Unix domain socket.

and when you do a

import asyncio

async def main():
	server = await asyncio.start_unix_server(aCallBack(),path='/tmp/aSocket')
	print(server)

the print give you

<Server sockets=(<asyncio.TransportSocket fd=6, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0, laddr=/tmp/aSocket>,)>

I recommend beginning with asyncio first, reading the documentation, trying out some examples, and so on.

Thanks, any good reference, guide, tutorials ?

I solely rely on Python documentation. You should become familiar with it and use it as a handbook.

1 Like