How to Download and Upload Files in FTP Server using Python?
Last Updated :
12 Jan, 2022
Prerequisite: FTP, ftplib
Here, we will learn how to Download and Upload Files in FTP Server Using Python. Before we get started, first we will understand what is FTP.
FTP(File Transfer Protocol)
File Transfer Protocol(FTP) is an application layer protocol that moves files between local and remote file systems. It runs on the top of TCP, like HTTP. To transfer a file, 2 TCP connections are used by FTP in parallel: control connection and data connection.
For uploading and downloading the file, we will use ftplib Module in Python. It is an in-built module in Python.
What is ftplib module?
This module defines the class FTP and a few related items. The FTP class implements the client-side of the FTP protocol. You can use this to write Python programs that perform a variety of automated FTP jobs, such as mirroring other FTP servers.
We will use a test FTP server, it is called DLPTEST and we are going to use the below text file for all operations:
Let’s Understand step by step implementation:
- Enter Required Information, the information will be available click here.
Python3
import ftplib
HOSTNAME = "ftp.dlptest.com"
USERNAME = "dlpuser@dlptest.com"
PASSWORD = "eUj8GeW55SvYaswqUyDSm5v6N"
|
Note: Password will change time to time, make sure you visit their website for the correct credentials.
Python3
ftp_server = ftplib.FTP(HOSTNAME, USERNAME, PASSWORD)
ftp_server.encoding = "utf-8"
|
- Upload the File (To upload a file, we will use storbinary() method)
Syntax:
# Store a file in binary transfer mode
storbinary(command, **)
Python3
filename = "gfg.txt"
with open (filename, "rb" ) as file :
ftp_server.storbinary(f "STOR {filename}" , file )
|
- Get the list of directories using dir() method. The test server will remove files after 30 minutes.
Output:
- Download the File (To download a file, we will use retrbinary() method.
Python3
filename = "gfg.txt"
with open (filename, "wb" ) as file :
ftp_server.retrbinary(f "RETR {filename}" , file .write)
|
- Close the FTP Connection.
Python3
file = open (filename, "r" )
print ( 'File Content:' , file .read())
ftp_server.quit()
|
Output:
Below is the complete program for uploading the file in FTP Server:
Python3
import ftplib
HOSTNAME = "ftp.dlptest.com"
USERNAME = "dlpuser@dlptest.com"
PASSWORD = "eUj8GeW55SvYaswqUyDSm5v6N"
ftp_server = ftplib.FTP(HOSTNAME, USERNAME, PASSWORD)
ftp_server.encoding = "utf-8"
filename = "File Name"
with open (filename, "rb" ) as file :
ftp_server.storbinary(f "STOR {filename}" , file )
ftp_server. dir ()
ftp_server.quit()
|
Output:
Below is the complete program for downloading the file in FTP Server:
Python3
import ftplib
HOSTNAME = "ftp.dlptest.com"
USERNAME = "dlpuser@dlptest.com"
PASSWORD = "eUj8GeW55SvYaswqUyDSm5v6N"
ftp_server = ftplib.FTP(HOSTNAME, USERNAME, PASSWORD)
ftp_server.encoding = "utf-8"
filename = "gfg.txt"
with open (filename, "wb" ) as file :
ftp_server.retrbinary(f "RETR {filename}" , file .write)
ftp_server. dir ()
file = open (filename, "r" )
print ( 'File Content:' , file .read())
ftp_server.quit()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...