Python Glossary Русский

Python 3.9+Linux · macOSsocketfunction

socket.recv_fds()

file descriptors and ancillary data

Receives data and up to maxfds file descriptors from an AF_UNIX socket: (msg, fds, flags, addr).

Syntax

msg, fds, flags, addr = socket.recv_fds(sock, bufsize, maxfds, flags=0)

The received descriptors are new numbers in your process and you must close them, or they leak. If more than maxfds were sent, the extra ones are dropped and MSG_CTRUNC appears in flags, so check it. As with recv, data need not arrive in full: bufsize is an upper bound, not a promise.

Examples

import os, socket
r1, w1 = os.pipe()
r2, w2 = os.pipe()
a, b = socket.socketpair()
with a, b:
    socket.send_fds(a, [b'x'], [w1, w2])
    msg, fds, flags, addr = socket.recv_fds(b, 16, 1)
    print(len(fds))  # → 1
    print(bool(flags & socket.MSG_CTRUNC))  # → True — второй дескриптор потерян
    for fd in [r1, w1, r2, w2, *fds]:
        os.close(fd)

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy