How To Download A File From A Ftp Server Using Python
Prerequisite: FTP , ftplib
Here, we volition acquire how to Download and Upload Files in FTP Server Using Python. Earlier we go started, first we volition 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. Information technology runs on the top of TCP, like HTTP. To transfer a file, 2 TCP connections are used by FTP in parallel: control connection and information connexion.
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 customer-side of the FTP protocol. You can utilize this to write Python programs that perform a variety of automated FTP jobs, such every bit mirroring other FTP servers.
We volition utilise a exam FTP server, it is called DLPTEST and nosotros are going to use the below text file for all operations:
Let's Understand step by footstep implementation:
- Enter Required Data, 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.
- Connect to Server
Python3
ftp_server
=
ftplib.FTP(HOSTNAME, USERNAME, Countersign)
ftp_server.encoding
=
"utf-8"
- Upload the File (To upload a file, we volition use storbinary() method)
Syntax:
# Store a file in binary transfer mode storbinary(command, **)
Python3
filename
=
"gfg.txt"
with
open
(filename,
"rb"
) every bit
file
:
ftp_server.storbinary(f
"STOR {filename}"
,
file
)
- Get the list of directories using dir() method. The examination server volition remove files afterwards 30 minutes.
Python3
Output:
- Download the File (To download a file, nosotros volition use retrbinary() method.
Python3
filename
=
"gfg.txt"
with
open up
(filename,
"wb"
) as
file
:
ftp_server.retrbinary(f
"RETR {filename}"
,
file
.write)
- Close the FTP Connection.
Python3
file
=
open up
(filename,
"r"
)
print
(
'File Content:'
,
file
.read())
ftp_server.quit()
Output:
Beneath is the consummate programme 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-eight"
filename
=
"File Name"
with
open
(filename,
"rb"
) equally
file
:
ftp_server.storbinary(f
"STOR {filename}"
,
file
)
ftp_server.
dir
()
ftp_server.quit()
Output:
Below is the complete plan for downloading the file in FTP Server:
Python3
import
ftplib
HOSTNAME
=
"ftp.dlptest.com"
USERNAME
=
"dlpuser@dlptest.com"
Countersign
=
"eUj8GeW55SvYaswqUyDSm5v6N"
ftp_server
=
ftplib.FTP(HOSTNAME, USERNAME, PASSWORD)
ftp_server.encoding
=
"utf-eight"
filename
=
"gfg.txt"
with
open up
(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:
Source: https://www.geeksforgeeks.org/how-to-download-and-upload-files-in-ftp-server-using-python/
Posted by: caballeroaraid1960.blogspot.com
0 Response to "How To Download A File From A Ftp Server Using Python"
Post a Comment