使用ftplib,轻松实现从ftp服务器上下载所需要的文件,包括目录结构等,支持了一下断点续传
1 from ftplib import FTP 2 import sys 3 import os 4 import re 5 6 def ftpconnet(ftpserver,port,username,password): 7 ftp = FTP() 8 try: 9 ftp.connect(ftpserver,port) 10 except: 11 raise IOError,'FTP connect failed!' 12 13 try: 14 ftp.login(username,password) 15 except: 16 raise IOError,'FTP login failed!' 17 else: 18 return ftp 19 20 def ftpdownload(ftp,ori_path,dest_path): 21 for each in ftp.nlst(ori_path): 22 try: 23 ftp.cwd(each) 24 except: 25 filename = re.search('\S+\/(\S+)',each).group(1) 26 local_file = dest_path + '/' + filename 27 if os.path.exists(local_file): 28 lsize = os.stat(local_file).st_size 29 rsize = ftp.size(each) 30 if lsize > rsize: 31 sys.stderr.write('the local file %s is bigger than the remote!\n'%local_file) 32 return False 33 elif lsize == rsize: 34 sys.stderr.write('the file %s has been completed!\n'%local_file) 35 bufsize = 1024 * 1024 36 fp = open(local_file,'ab') 37 ftp.retrbinary('RETR '+each,fp.write,bufsize,rest=lsize) 38 else: 39 bufsize = 1024 * 1024 40 fp = open(local_file,'wb') 41 ftp.retrbinary('RETR '+each,fp.write,bufsize) 42 else: 43 dirname = re.search('\S+\/(\S+)',each).group(1) 44 dirname = dest_path + '/' + dirname + '/' 45 os.system('mkdir -p %s'%dirname) 46 ftpdownload(ftp,each,dirname) 47 48 def ftpclose(ftp): 49 ftp.quit() 50 51 if __name__ == '__main__': 52 ftp = ftpconnet('climb.genomics.cn',21,'','') 53 ftpdownload(ftp,'/pub/10.5524/100001_101000/100145/','./') 54 ftpclose(ftp)