Python 3.4+Linux · macOSAsync programmingfunction
asyncio.start_unix_server()
streams
Starts a server on a Unix domain socket, a file in the file system; the callback gets a (StreamReader, StreamWriter) pair.
Syntax
server = await asyncio.start_unix_server(client_connected_cb, path=None, *, limit=2**16, **kwds)
The socket file is created on start, and before 3.13 it stayed on disk after closing: the next run failed with OSError "Address already in use" until you deleted the file by hand. Since 3.13 cleanup_socket=True removes it for you. Access is governed by file permissions, so only users allowed to open the file can connect; the network is not involved at all. Available on Linux and macOS only: asyncio has no Unix sockets on Windows.
Examples
import asyncio
import os
async def upper(reader, writer):
writer.write((await reader.read(10)).upper())
await writer.drain()
writer.close()
async def main():
server = await asyncio.start_unix_server(upper, 'app.sock')
print(os.path.exists('app.sock')) # → True — сокет это файл
async with server:
reader, writer = await asyncio.open_unix_connection('app.sock')
writer.write(b'ok')
print(await reader.read()) # → b'OK'
writer.close()
await writer.wait_closed()
if os.path.exists('app.sock'): # до 3.13 файл остаётся
os.remove('app.sock')
asyncio.run(main())
See also
Open in the full glossarydocs.python.orgReport an inaccuracy