python FTP 下载全文件
搭了一个ftp站点,没想到的是利用客户端的时候,服务器当掉了,于是自己写了一个下载ftp站点的脚本,脚本不大,不超过30行。可能利用了一点函数式的编程思想吧。
源代码:
?
#/usr/bin/env python import ftplib, sys, oslocal_root = '/home/calvin/learn/tsace'host = 'localhost'username = 'jesse'passewd = '123456'f = FalsewriteFile = lambda filename: open(filename, 'w').writegetcwd = lambda curwd: curwd == '/' and '/' or (curwd + '/')createDir = lambda dirname: not os.path.exists(dirname) and os.mkdir(dirname)def isDirectory(filename): try: f.cwd(filename) createDir(local_root+filename) return True except: return Falsedef recursiveDownload(filelist, curpwd): global local_root map(lambda file: isDirectory(getcwd(curpwd) + file) and [recursiveDownload(f.nlst(), f.pwd())] or f.retrlines('RETR '+ (getcwd(curpwd) + file), writeFile(local_root + getcwd(curpwd) + file)), filelist)if __name__ == '__main__': f = ftplib.FTP(host) resp = f.login(username, passewd) recursiveDownload(f.nlst(), f.pwd()); f.quit()