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.
Here, showcasing the information using a test FTP server, DLPTEST and python’s built-in ftplib module.
import ftplib
FTP_HOST = "ftp.dlptest.com"
FTP_USER = "host_username"
FTP_PASS = "password"
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 & directories
ftp.dir()
Output:
Downloading files
Let’s download the same uploaded file.
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 & directories
ftp.dir()
# quit and close the connection
ftp.quit()
Download file in FTP Server using Python.
Ftp_file_downloader.py
import ftplib
FTP_HOST = "ftp.dlptest.com"
FTP_USER = "host_username"
FTP_PASS = "password"
# connect to the FTP server
ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)
# force UTF-8 encoding
ftp.encoding = "utf-8"
# the name of file you want to download from the FTP server
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)
# quit and close the connection
ftp.quit()
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.