Python Glossary Русский

Python 3.9+Linux · macOSsocketfunction

socket.send_fds()

file descriptors and ancillary data

Passes open file descriptors to another process over an AF_UNIX socket, together with ordinary data.

Syntax

socket.send_fds(sock, buffers, fds, flags=0, address=None) -> int

What travels is the open file itself, not the number: the receiver gets a different descriptor number pointing at the same kernel object. The sender must close its own descriptors afterwards, the function does not. buffers must not be empty: without at least one data byte, ancillary data is not delivered on some systems. It works only on Unix and only for AF_UNIX.

Examples

import os, socket
r, w = os.pipe()
a, b = socket.socketpair()
with a, b:
    socket.send_fds(a, [b'pipe'], [w])
    msg, fds, flags, addr = socket.recv_fds(b, 1024, 1)
    print(msg, len(fds))  # → b'pipe' 1
    os.write(fds[0], b'ok')
    print(os.read(r, 2))  # → b'ok'
    for fd in [r, w, *fds]:
        os.close(fd)

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy