Send_fds() Function Of Socket Module In Python


  • The function socket.send_fds(), sends file descriptors from one process to another using Unix Domain Sockets - the internals of which are detailed here.

  • Using the socket interface, any two machines that talk TCP/IP can communicate with each other.

  • Unix Domain Protocol uses the same socket interface used for network communication while communicating to a process running in the same machine.

  • Since the Unix domain protocol communicates between two processes in the same machine, traversing the TCP/IP suite at both ends of the communication is avoided which reduces the latency significantly.

  • In Unix, file descriptors can be be passed between two processes using Unix Domain Sockets.

  • When file descriptors are passed through Unix Domain Sockets, Unix recreates the descriptors sent to the other process through kernel. It takes care of the corner cases like closing the handle from the client process before the server receiving it.

  • The socket.sendmsg() is the basic function that takes care of sending all kind information between two sockets, be it TCP/IP or Unix Domain Protocol. The socket.receivemsg() function does the receiving work on client and server sockets.

  • The socket.send_fds() uses socket.sendmsg() to send file descriptors from one Unix domain socket to another.

Example - Client sending file descriptors through a Unix domain socket:

# Example Python program that uses domain
# sockets and the socket module function
# send_fds() to send multiple file
# descriptors to another process.
import socket
import os

# Create a UNIX Domain socket
sender = socket.socket(socket.AF_UNIX)

# Connect the socket to the server
path = "/Users/ex/sender_fd"

# Obtain two file descriptors both in read only mode
fd1 ="./news1.txt", os.O_RDONLY)
fd2 ="./news2.txt", os.O_RDONLY)

# Append the descriptors to a list 
desList = []

# Send the list of file descriptors to the server
msg     = "hi" 
msgb    = bytes(msg, 'ascii')
socket.send_fds(sender, [msgb], desList, 2, None)

# Receive data from the Unix Domain Socket
    data = sender.recv(1024)
        print("Server Response:")
        print("Connection closed by server")



Server Response:

b'Great...have read all the news'

Connection closed by server

Example - Server receiving file descriptors through a Unix domain socket:

# Example Python program that creates 
# a server using an Unix Domain Socket  
# and receives File Descriptors from other
# processes connecting to this server through 
# the function socket.receive_fds()  
import socket
import os

# Create a Unix Domain socket
domainServer = socket.socket(socket.AF_UNIX)

# Bind the socket to a path
path = "/Users/ex/sender_fd"

# Enter into listening mode
print("Unix domain socket waiting for client connections...")

# Start accepting connections
    sock, addr = domainServer.accept()

    # Read file descriptors from a connection
        tup = socket.recv_fds(sock, 1024, 2)
        fdList = tup[1]


        # Read from each file descriptor
        # and print the contents
        for fd in fdList:
            print(, 8))

        sock.send("Great...have read all the news".encode())


Unix domain socket waiting for client connections...




Copyright 2023 ©