Python uses SFTP and FTP to download files from the server
- 2021-08-28 20:44:35
- OfStack
1. Connect the remote server with the ftplib module
Common Methods of ftplib Module
ftp Login connection
from ftplib import FTP # Loading ftp Module
ftp=FTP() # Set variables
ftp.set_debuglevel(2) # Turn on the debug level 2 Display details
ftp.connect("IP","port") # Connected ftp sever And ports
ftp.login("user","password") # User name and password for connection
print ftp.getwelcome() # Print out the welcome message
ftp.cmd("xxx/xxx") # Enter the remote directory
bufsize=1024 # Set buffer size
filename="filename.txt" # Files to be downloaded
file_handle=open(filename,"wb").write # Open a file locally in write mode
ftp.retrbinaly("RETR filename.txt",file_handle,bufsize) # Receive files on the server and write to local files
ftp.set_debuglevel(0) # Turn off debug mode
ftp.quit() # Quit ftp
ftp Related command operation
ftp.cwd(pathname) # Settings FTP The path of the current operation
ftp.dir() # Display all directory information under the directory
ftp.nlst() # Get the files in the directory
ftp.mkd(pathname) # New Remote Directory
ftp.pwd() # Return to the current location
ftp.rmd(dirname) # Delete Remote Directory
ftp.delete(filename) # Delete remote files
ftp.rename(fromname, toname)# Will fromname Modify the name to toname .
ftp.storbinaly("STOR filename.txt",file_handel,bufsize) # Upload target file
ftp.retrbinary("RETR filename.txt",file_handel,bufsize) # Download FTP Documents
ftplib module encapsulation call
from ftplib import FTP
from common.logModule import LogClass
class FTPClass(LogClass):
"""
:FTP Connect to a remote server to download files
"""
def __init__(self, ftp_link, ftp_path=None):
"""
: Connect ftp Server
:param ftp_link: Server ip, User name , Password
:param ftp_path: ftp File path in
"""
LogClass.__init__(self, logName='FTPClass')
self.ftp = FTP(ftp_link[0]) # Link FTP
self.ftp.set_debuglevel(2) # Setting Debug Levels
self.ftp.login(ftp_link[1], ftp_link[2]) # Enter a user name and password
self.ftp.set_pasv(False) # False: Active mode True Passive mode
if ftp_path:
self.ftp.cwd(ftp_path)
def download_file(self, like_file_name, save_path, save_name=None):
"""
: Download the file and save it locally, if save_name If it is blank, download all file names containing like_file_name All files of
:param like_file_name: Files to be downloaded
:param save_path : File save path
:param save_name: File save name
:return:
"""
try:
ftp_files = self.ftp.nlst() # Get ftp All file names in the current directory
if save_name: # If the file save name exists, only download the list 1 Documents
fp = open(save_path + save_name, 'wb') # Open a file locally in write mode
self.ftp.retrbinary('RETR ' + 'ftp Filename ', fp.write) # Receive files on the server and write to local files
fp.close() # Close a file
else: # Download the file containing the name if the file is saved like_file_name All files of
for ftp_file in ftp_files:
if ftp_file[-5:] == '.GRB2':
if like_file_name in ftp_file: # If the file name contains like_file_name
fp = open(save_path + ftp_file, 'wb') # Open a file locally in write mode
self.ftp.retrbinary('RETR ' + ftp_file, fp.write) # Receive files on the server and write to local files
fp.close() # Close a file
self.ftp.quit() # Shut down ftp Connect
except Exception:
raise
if __name__ == '__main__':
ftp_link = ['ip', 'username', 'password']
ftp_path = '/data/result/'
ftp = FTPClass(ftp_link, ftp_path)
like_file_name = ".GRB2"
save_path = 'D:\\file\\'
ftp.download_file(like_file_name, save_path)
Connect Remote Server with paramiko Module
Common Methods of paramiko Module
put(self,localpath,remotepath,callback=None,confirm=True)
Parameter description:
localpath Local path to upload source files
remotepath : Destination path
callback Gets the number of bytes received and the total number of bytes transmitted
confirm Whether to call after uploading stat() Method to confirm the file size
get(self, remotepath, localpath, callback=None)
Parameter description:
remotepath Remote Files to Download
localpath : Local storage path
callback Same as put Method
mkdir Used to create directories
remove : Delete Directory
rename : Rename
stat Getting file information
listdir Getting a list of directories
paramiko module encapsulation call
import os
import paramiko
from stat import S_ISDIR
from common.logModule import LogClass
class SFTPClass(LogClass):
"""
:SFTP Connect to a remote server to download files
"""
def __init__(self, ftp_link, ftp_path=None, local_path=None):
"""
Connect ftp Server
"""
LogClass.__init__(self, logName='FTPProcess')
trans = paramiko.Transport((ftp_link[0], 22)) # Connect ftp
trans.connect(username=ftp_link[1], password=ftp_link[2]) # Enter a user name and password
self.sftp = paramiko.SFTPClient.from_transport(trans)
self.ftp_path = ftp_path # Target path
self.local_path = local_path # Save path
def save_all_file_path(self, ftp_path):
""" Save a list of all files """
all_files = list()
if ftp_path[-1] == '/': # Remove the last character of the path string '/' , if any
ftp_path = ftp_path[0:-1]
files = self.sftp.listdir_attr(ftp_path) # Gets all directories and files under the current specified directory, including attribute values
for i in files:
filename = ftp_path + '/' + i.filename
if S_ISDIR(i.st_mode): # If it is a directory, it is processed recursively, which is used here stat In the library S_ISDIR Method
all_files.extend(self.save_all_file_path(filename))
else:
all_files.append(filename)
return all_files
def download_file(self):
"""
Download the file and save it locally
"""
try:
if self.ftp_path and self.local_path:
all_files = self.save_all_file_path(self.ftp_path) # Save a list of all files
for file in all_files:
filename = file.split('/')[-1]
local_filename = os.path.join(self.local_path, filename)
self.logger.info(u'During file download: %s' % filename)
self.sftp.get(file, local_filename) # Download to local
else:
self.logger.error("ftp_path or local_path is null")
return
except Exception as e:
self.logger.error(e)
if __name__ == '__main__':
ftp_link = ['ip', 'user', 'password']
ftp_path = "/data/"
local_path = "D:\\file\\"
f = SFTPClass(ftp_link, ftp_path=None, local_path=None)
f.download_file()