attach_file

    How to Download and Upload Files in FTP Server using Python?

    Categories:

    ftp-server-using-python

    This blog will teach you how to download and upload files in an FTP server using python. Before moving further, let’s briefly examine what FTP is.

    What is an FTP?

    FTP stands for Files Transfer Protocol. FTP’s main feature is transferring files from local to remote using TCP connections. FTP is also called application layer protocol. FTP has two processes for the transforming process: data connection and control connection.

    As per today’s topic, we will use the ftplib module of python in the FTP server for downloading and uploading files. Let’s have a brief about the ftplib module.

    Describe the ftplib module.

    Python has a module called ftplib to transfer files in the FTP server. With the help of ftplib, you can connect to the client-side FTP server and transfer files. As a Django development company, you might frequently need to upload and download files in your projects, making this module indispensable. Also, you can download and upload files in the FTP server using Python’s ftplib module.

    Implementation Guide

    Here, we showcase the process using a test FTP server, DLPTEST, and Python’s built-in ftplib module. For any Python development company aiming to provide robust solutions, understanding how to work with FTP servers is vital. By following this guide, you’ll be well-equipped to handle file transfers efficiently in your projects.

    import ftplib
    
    FTP_HOST = "ftp.dlptest.com"
    FTP_USER = "<host_username>";
    FTP_PASS = "password"
    </host_username>

    The above code tends to take credentials to the FTP_User. The password can be changed from time to time. While making changes, make sure you are visiting their website to correct it.

    The following step code is to establish the connection with FTP_SERVER.

    #connect to FTP Server
    
    ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)
    ftp.encoding = "utf-8"

    Uploading Files

    Uploading the file using FTP Server is shown below. The local name to identify the file is “Some_file”.

    filename = "some_file.txt"
    with open(filename, "rb") as file:
        # use FTP's STOR command to upload the file
        ftp.storbinary(f"STOR {filename}", file)

    After this code, the file starts to upload to FTP_SERVER. And to upload, we have used the STOR command.
    Also, “rb” opens the file as read-only in binary format and starts reading from the beginning of the file. While the binary form can be used for various purposes.
    As we are aware that the test server will delete the files after 30 minutes, so to make sure the files are uploaded, we will list the files and directories using the below code.

    # list current files &amp; directories
    ftp.dir()

    Downloading files

    filename = "some_file.txt"
    with open(filename, "wb") as file:
        # use FTP's RETR command to download the file
        ftp.retrbinary(f"RETR {filename}", file.write)

    We will download with the command “wb”, as it will write the file from FTP_SERVER to the local machine.

    Here, “wb” opens the file as write-only in binary format.

    Using the RETR command, which downloads a copy of a file from the server, we can request a copy of a file by giving the command the name of the file we wish to download as the first argument.

    The second argument to the ftp.retrbinary() method specifies the procedure when saving the file to the local machine.

    The file can reappear even after deleting it as we rerun the code, proving the successful downloading process.

    Now to close the FTP server connection, the last code will be:

    #quit and close the connection
    ftp.quit()

    Codes for Uploading & Downloading files in FTP Server

    Upload file in FTP Server using Python.

    Ftp_file_uploader.py
    
    import ftplib
    #FTP Server Crendentials
    FTP_HOST = "ftp.dlptest.com"
    FTP_USER = "<host_username>"
    FTP_PASS = "password"
    
    #connect to FTP Server
    ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)
    ftp.encoding = "utf-8"
    
    # local file name you want to upload
    filename = "some_file.txt"
    with open(filename, "rb") as file:
        # use FTP's STOR command to upload the file
        ftp.storbinary(f"STOR {filename}", file)
    
    # list current files &amp; directories
    ftp.dir()
    
    # quit and close the connection
    ftp.quit()s
    </host_username>

    Download file in FTP Server using Python.

    Conclusion:

    Wrapping up here with the above-given codes to upload and download files in FTP Server using Python. To conclude, the code is running perfectly with the output shared above.

    Our Portfolio

    Our Incredible Portfolio Across Various Industry Verticals

    Ready to start your dream project?

    We have a TEAM to get you there.