Get file code from FTP, their own encapsulation of a class, of course, also have a download method, interested in their own take a look, can be extended according to their own needs, in the outside call best use try except finally form, or with XXX as form, Be sure to call quit at the end of your custom method to close the connection.

from ftplib import FTP from oslo_log import log import os import time LOG = log.getLogger(__name__) # define time sys_time = time.time() sys_time_array = time.localtime(sys_time) current_time = time.strftime("%Y-%m-%d %H:%M:%S:", sys_time_array) class FTPUtils(FTP): def __init__(self, host, port, user, passwd): self.port = port super(FTPUtils, self).__init__(host=host, user=user, passwd=passwd) def search_dir(self, ftp, start_dir, ftp_dir_l): dir_res = [] ftp.cwd(start_dir) ftp_dir_l.append(ftp.pwd()) ftp.dir('.', dir_res.append) for i in dir_res: if i.startswith("d"): self.search_dir(ftp, ftp.pwd() + "/" + i.split(" ")[-1], ftp_dir_l) ftp.cwd('.. ') return ftp_dir_l def search_file(self, dir_path): ftp_dir_l = [] ftp_file_l = [] ftp_dir_l = self.search_dir(self, dir_path, ftp_dir_l) for server_f_l in ftp_dir_l: file_list = self.nlst(server_f_l) for server_file in file_list: if server_file not in ftp_dir_l: ftp_file_l.append(server_file) return ftp_file_l def ftp_download(self, remote_path, local_path): try: file_list = self.nlst(remote_path) except Exception: LOG.error("remote_path error.") else: key = os.path.exists(local_path) if str(key) == 'True': pass else: os.makedirs(local_path) try: for file in file_list: bufsize = 1024 file_name = file.split('/')[-1] local_file = open(local_path + file_name, 'wb') self.retrbinary('RETR %s' % (file), local_file.write, bufsize) self.set_debuglevel(0) local_file.close() except Exception: LOG.error("%s %s download failed." % (current_time, remote_path)) else: LOG.error("%s %s download successfully." % (current_time, remote_path)) def ftp_upload(self, remote_path, local_path): try: self.mkd(remote_path) except Exception: pass try: file_list = os.walk(local_path) for root, dirs, files in file_list: for file in files: local_file = local_path + file remote_file = remote_path + file bufsize = 1024 fp = open(local_file, 'rb') self.storbinary('STOR ' + remote_file, fp, bufsize) fp.close() except Exception: LOG.error("%s %s upload failed." % (current_time, local_path)) else: LOG.error("%s %s upload successfully." % (current_time, local_path)) def remove_local_file(self, local_file_path): if os.path.exists(local_file_path): os.remove(local_file_path) else: LOG.error('no such file:%s' % local_file_path.split('/')[-1]) def _dir_list_name(self): paths = self.nlst(self.pwd()) return [path[path.rfind('/') + 1:] for path in paths] def _dir_list_path(self): lines = self.nlst(self.pwd()) return linesCopy the code

Some functions of FTP are explained as follows

FTP =FTP() # set variable ftp.set_debuglevel(2) # enable debuglevel 2, FTP. Connect ("IP","port") # FTP. Login ("user","password") # FTP. CMD (" XXX/XXX ") # Go to the remote directory bufsize=1024 # Set buffer size filename="filename.txt" # File to download File_handle =open(filename,"wb").write # Open file locally in write mode ftp.retrbinaly("RETR filename.txt",file_handle,bufsize) Set_debuglevel (0) # Disable debug mode ftp.quit() # Exit FTP FTP related commands operation ftp.cwd(pathname) # Set FTP current operation path ftp.dir() MKD (pathname) # Create remote directory ftp.pwd() # Return current location ftp.rmd(dirname) # Delete remote directory ftp.nlst() # Delete remote directory ftp.mkd(pathname) # Delete remote directory ftp.rmd(dirname Rename (fromname, toname)# rename fromname to toname. RETR filename.txt ("RETR filename.txt",file_handel,bufsize) # Download FTP fileCopy the code