孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

搭了一个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()
posted on 2012-08-31 09:59  孤独的猫  阅读(333)  评论(0编辑  收藏  举报