当前位置:编程学习 > python >>

python FTP下载全文件

搭了一个ftp站点,没想到的是利用客户端的时候,服务器当掉了,于是自己写了一个下载ftp站点的脚本,脚本不大,不超过30行。可能利用了一点函数式的编程思想吧。
源代码:
 
Python代码 
#/usr/bin/env python 
   
import ftplib, sys, os 
 
local_root = '/home/calvin/learn/tsace' 
host = 'localhost' 
username = 'jesse' 
passewd = '123456' 
f = False 
writeFile = lambda filename: open(filename, 'w').write 
getcwd = 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 False 
def 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() 

补充:Web开发 , Python ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,