sendfile() function of os module in Python

The sendfile() function copies the contents of one file to another file using their descriptors. In the Python example, both a TCP server and its clients sends files to each other using the sendfile() of the os module.

Os Updated 5 May 2023 2 min read

Function

sendfile(out_fd, in_fd, offset, count)

Overview

  • The sendfile() function copies the contents of one file to another file, using their descriptors.

Parameters

out_fd - The descriptor of the destination file

in_fd - The descriptor of the source file

offset - Offset from where the contents need to be copied from the source

count - Number of bytes to be copied

Return Value

None

Example Client Program

python
# Example client program that sends the text of a song

            # from a file over a streaming socket to the server

            import os

            import socket

# The file to be sent and its descriptor

            srcPath = "/PythonProgs/BaaBaa.txt"

            srcDesc     = os.open(srcPath, os.O_RDONLY, 0o744)

# Create a client connection

            socketObject = socket.socket()

            socketObject.connect(("localhost", 34422))

# In macOS, a zero value for the count means

            # to send all the file contents

            os.sendfile(socketObject.fileno(), srcDesc, 0, 0)

            data = socketObject.recv(2048)

            print(data)

Output

Output
b'The wheels on the bus go round and round'

Example - Server Program

python
# Example server program that sends

            # a song to its client connections using

            # os.sendfile() function

            import socket

            import os

# Create a streaming server socket(i.e,TCP server) that

            # uses Internet Protocol v4 addressing scheme

            svr = socket.socket()

            print("Server socket created")

ip      = "127.0.0.1"

            port    = 34422

            svr.bind((ip, port))

            print("Server bound with ip {} port {}".format(ip, port))

# Listen for connection requests

            svr.listen()

# Serve connection requests

            count = 0

while(True):

                (client, clientIP) = svr.accept()

                count = count + 1

                print("Accepted {} connections till now".format(count))

                while(True):

                    # Recv msgs upto 1 KB

                    data = client.recv(1024)

                    print(data)

                    if(data!=b''):

                        # Send the song, "Wheels on the bus..."" as reply to clients

                        srcFilePath = "/PythonProgs/Wheels.txt"

                        srcDesc  = os.open(srcFilePath, os.O_RDONLY, 0o744)

                        # Get the file descriptor associated with the socket

                        clientDesc = client.fileno()

                        os.sendfile(clientDesc, srcDesc, 0, 0)

                        client.close()

                        print("Client connection closed")

                        break

Output

Output
Server socket created

            Server bound with ip 127.0.0.1 port 34422

            Accepted 1 connections till now

            b'Baa, baa, black sheep,\nHave you any wool?\nYes, sir, yes, sir,\nThree bags full;\nOne for my master,\nAnd one for my dame,\nand one for the little boy\nWho lives down the lane.'

            Client connection closed