Code of the rest of the code, the code process of some important code segments to backup, the following code content is about Python from the ftp server to download the file of the code, I hope to be able to small partners have the use of.
#coding=utf-8 ''' ftp autodownload, autoupload script with recursive directory manipulation ''' from ftplib import FTP import os,sys,string,datetime,time import socket class MYFTP: def __init__(self, hostaddr, username, password, remotedir, port=21): = hostaddr = username = password = remotedir = port = FTP() self.file_list = [] # .set_debuglevel(2) def __del__(self): () # .set_debuglevel(0) def login(self): ftp = try: timeout = 300 (timeout) ftp.set_pasv(True) print u'Starting connection to %s' %() (, ) print u'Successfully connected to %s' %() print u'Start login to %s' %() (, ) print u'Successfully logged in to %s' %() debug_print(()) except Exception: print u'Connection or login failed' try: () except(Exception): print u'Failed to switch directories' def is_same_size(self, localfile, remotefile): try: remotefile_size = (remotefile) except: remotefile_size = -1 try: localfile_size = (localfile) except: localfile_size = -1 debug_print('localfile_size:%d remotefile_size:%d' %(localfile_size, remotefile_size),) if remotefile_size == localfile_size: return 1 else: return 0 def download_file(self, localfile, remotefile): if self.is_same_size(localfile, remotefile): debug_print(u'%s Same file size, no download required' %localfile) return else: debug_print(u'>>>>>>>>>>>>>>>>>>>>>>>>Download file %s ... ...' %localfile) #return file_handler = open(localfile, 'wb') (u'RETR %s'%(remotefile), file_handler.write) file_handler.close() def download_files(self, localdir='./', remotedir='./'): try: (remotedir) except: debug_print(u'Directory %s does not exist, continue...' %remotedir) return if not (localdir): (localdir) debug_print(u'Switch to directory %s' %()) self.file_list = [] (self.get_file_list) remotenames = self.file_list #print(remotenames) #return for item in remotenames: filetype = item[0] filename = item[1] local = (localdir, filename) if filetype == 'd': self.download_files(local, filename) elif filetype == '-': self.download_file(local, filename) ('..') debug_print(u'Return to upper directory %s' %()) def upload_file(self, localfile, remotefile): if not (localfile): return if self.is_same_size(localfile, remotefile): debug_print(u'Skip [equal]: %s' %localfile) return file_handler = open(localfile, 'rb') ('STOR %s' %remotefile, file_handler) file_handler.close() debug_print(u'Transmitted: %s' %localfile) def upload_files(self, localdir='./', remotedir = './'): if not (localdir): return localnames = (localdir) (remotedir) for item in localnames: src = (localdir, item) if (src): try: (item) except: debug_print(u'Directory already exists %s' %item) self.upload_files(src, item) else: self.upload_file(src, item) ('..') def get_file_list(self, line): ret_arr = [] file_arr = self.get_filename(line) if file_arr[1] not in ['.', '..']: self.file_list.append(file_arr) def get_filename(self, line): pos = (':') while(line[pos] != ' '): pos += 1 while(line[pos] == ' '): pos += 1 file_arr = [line[0], line[pos:]] return file_arr def debug_print(s): print s if __name__ == '__main__': timenow = () datenow = ('%Y-%m-%d', timenow) # Configure the following variables hostaddr = '211.15.113.45' # ftp address username = 'UserName' # Username password = '123456' # Password port = 21 # Port number rootdir_local = 'E:/mypiv' # Local Catalog rootdir_remote = '/PIV' # Remote directory f = MYFTP(hostaddr, username, password, rootdir_remote, port) () f.download_files(rootdir_local, rootdir_remote) timenow = () datenow = ('%Y-%m-%d', timenow) logstr = u"%s Successfully executed backup n" %datenow debug_print(logstr)
This is the whole content of this article.