搭了一个ftp站点,没想到的是利用客户端的时候,服务器当掉了,于是自己写了一个下载ftp站点的脚本,脚本不大,不超过30行。可能利用了一点函数式的编程思想吧。
源代码:
#/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()
本文介绍如何使用Python编写一个脚本来下载FTP站点的内容,包括登录验证、目录遍历和文件下载等功能。
1278

被折叠的 条评论
为什么被折叠?



