recv_fds() function of socket module in Python

The recv_fds() functions receives file descriptors from a server socket using Unix Domain Protocol. In the Python example, the client sends the file names to the server and gets the descriptors to read from files and to print onto the console.

socket Updated 19 Nov 2022 2 min read

Overview

  • The socket module function recv_fds() receives file descriptors sent from a Unix Domain Socket. To send file descriptors refer to send_fds().
  • The file descriptors are reconstructed by the Unix kernel for them to work as expected at the process where they are received.

Example - Client

python
# Example Python program that requests for

            # file descriptors from a server using Unix domain sockets

            # and the function socket.recv_fds()

            # to read & print the contents from the files.

            import socket

            import os

# Create a Unix Domain Socket

            client    = socket.socket(socket.AF_UNIX)

# Bind the Unix Domain socket to a path

            path     = "/Users/vinodh/pprogs/recv_fds"

            client.connect(path)

# Send the file names for which the descriptors

            # are needed from server

            client.send("./courses.txt,./campusinfo.txt,./contacts.txt".encode())

# Receive the file descriptors

            buf_size = 1024

            tup = socket.recv_fds(client, buf_size, 3)

# File descriptor list

            fdList = tup[1]

            print(tup)

# Read from each file descriptor

            # and print the contents

            bytesCount = 16

            for fd in fdList:

                print(os.read(fd, bytesCount))

Output

Output
(b'Sending the files you asked for->', [4, 5, 6], 0, None)

b'Course1\nCourse2\n'

b'Campus1\nCampus2'

b'Contacts\n'

Example - Server

python
# Example Python program that sends back file

            # descriptors as requested by clients

            # using Unix Domain Protocol

            import socket

            import os

# Create a Unix Domain Socket

            svr     = socket.socket(socket.AF_UNIX)

# Bind the Unix Domain Socket to a path

            path     = "/Users/vinodh/pprogs/recv_fds"

            os.unlink(path)

            svr.bind(path)

# Move the socket to listen state

            svr.listen()

# Accept connections from clients

            while(True):

                print("Server listening for file (descriptor) requests:")

                clSocket, address = svr.accept()

    # Get the names of the files

                fileNameReceived     = clSocket.recv(1024).decode()

                fileNames             = fileNameReceived.split(",")

                print(fileNames)

    fdList = []

                for fileName in fileNames:

                    print(fileName)

                    fd = os.open(fileName, os.O_RDONLY)

                    fdList.append(fd)

                # Send the file descriptors

                msg     = "Sending the files you asked for->"

                msgb    = bytes(msg, 'ascii')

                socket.send_fds(clSocket, [msgb], fdList, 3, None)

                clSocket.close()

Output

Output
Server listening for file (descriptor) requests:

['./courses.txt', './campusinfo.txt', './contacts.txt']

./courses.txt

./campusinfo.txt

./contacts.txt

Server listening for file (descriptor) requests: