1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
import os import sys import ftplib import traceback
class FtpDownloader(object): PATH_TYPE_UNKNOWN = -1 PATH_TYPE_FILE = 0 PATH_TYPE_DIR = 1
def __init__(self, host, user=None, passwd=None, port=21, timeout=10): self.conn = ftplib.FTP( host=host, user=user, passwd=passwd, timeout=timeout )
def dir(self, *args): ''' by defualt, ftplib.FTP.dir() does not return any value. Instead, it prints the dir info to the stdout. So we re-implement it in FtpDownloader, which is able to return the dir info. ''' info = [] cmd = 'LIST' for arg in args: if arg: cmd = cmd + (' ' + arg) self.conn.retrlines(cmd, lambda x: info.append(x.strip().split())) return info
def tree(self, rdir=None, init=True): ''' recursively get the tree structure of a directory on FTP Server. args: rdir - remote direcotry path of the FTP Server. init - flag showing whether in a recursion. ''' if init and rdir in ('.', None): rdir = self.conn.pwd() tree = [] tree.append((rdir, self.PATH_TYPE_DIR))
dir_info = self.dir(rdir) for info in dir_info: attr = info[0] name = info[-1] path = os.path.join(rdir, name) if attr.startswith('-'): tree.append((path, self.PATH_TYPE_FILE)) elif attr.startswith('d'): if (name == '.' or name == '..'): continue tree.extend(self.tree(rdir=path,init=False)) else: tree.append(path, self.PATH_TYPE_UNKNOWN)
return tree
def downloadFile(self, rfile, lfile): ''' download a file with path %rfile on a FTP Server and save it to locate path %lfile. ''' ldir = os.path.dirname(lfile) if not os.path.exists(ldir): os.makedirs(ldir) f = open(lfile, 'wb') self.conn.retrbinary('RETR %s' % rfile, f.write) f.close() return True
def treeStat(self, tree): numDir = 0 numFile = 0 numUnknown = 0 for path, pathType in tree: if pathType == self.PATH_TYPE_DIR: numDir += 1 elif pathType == self.PATH_TYPE_FILE: numFile += 1 elif pathType == self.PATH_TYPE_UNKNOWN: numUnknown += 1 return numDir, numFile, numUnknown
def downloadDir(self, rdir='.', ldir='.', tree=None, errHandleFunc=None, verbose=True): ''' download a direcotry with path %rdir on a FTP Server and save it to locate path %ldir. args: tree - the tree structure return by function FtpDownloader.tree() errHandleFunc - error handling function when error happens in downloading one file, such as a function that writes a log. By default, the error is print to the stdout. ''' if not tree: tree = self.tree(rdir=rdir, init=True) numDir, numFile, numUnknown = self.treeStat(tree) if verbose: print 'Host %s tree statistic:' % self.conn.host print '%d directories, %d files, %d unknown type' % ( numDir, numFile, numUnknown )
if not os.path.exists(ldir): os.makedirs(ldir) ldir = os.path.abspath(ldir)
numDownOk = 0 numDownErr = 0 for rpath, pathType in tree: lpath = os.path.join(ldir, rpath.strip('/').strip('\\')) if pathType == self.PATH_TYPE_DIR: if not os.path.exists(lpath): os.makedirs(lpath) elif pathType == self.PATH_TYPE_FILE: try: self.downloadFile(rpath, lpath) numDownOk += 1 except Exception as err: numDownErr += 1 if errHandleFunc: errHandleFunc(err, rpath, lpath) elif verbose: print 'An Error occurred when downloading '\ 'remote file %s' % rpath traceback.print_exc() print if verbose: print 'Host %s: %d/%d/%d(ok/err/total) files downloaded' % ( self.conn.host, numDownOk, numDownErr, numFile ) elif pathType == self.PATH_TYPE_UNKNOWN: if verbose: print 'Unknown type romote path got: %s' % rpath
if verbose: print 'Host %s directory %s download finished:' % ( self.conn.host, rdir ) print '%d directories, %d(%d failed) files, %d unknown type.' % ( numDir, numFile, numDownErr, numUnknown ) return numDir, numFile, numUnknown, numDownErr
if __name__ == '__main__': import sys import traceback from pprint import pprint as pr
flog = open('err.log', 'wb')
def run(host): try: fd = FtpDownloader( host=host, user='test', passwd='test', port=21, timeout=10 ) numDir, numFile, numUnknown, numDownErr = fd.downloadDir( rdir='.', ldir='download', tree=None, errHandleFunc=None, verbose=True ) flog.write( '%s\nok\n' '%d directories, %d(%d failed) files, %d unknown type\n\n\n' % ( host, numDir, numFile, numDownErr, numUnknown ) ) except Exception as err: traceback.print_exc() flog.write( '%s\nerror\n%s\n\n\n' % ( host, traceback.format_exc() ) )
pr(run(sys.argv[1])) flog.close()
|