python 實現ftp檔案下載
阿新 • • 發佈:2019-02-06
python 版本 2.7
程式碼:
#-*-coding:utf-8-*- import os import time from ftplib import FTP #伺服器地址 FTP_SERVER='xx.xx.xx.xx' -- 對應 ftpe伺服器地址 USER='使用者' PWD ='密碼' FTP_PATH='/home/xx/xx/' local_root='e:'+FTP_PATH DATE= time.strftime('%Y%m%d',time.localtime(time.time())) print DATE def isDir(filename): try: path = filename; path.replace('/','\\') if os.path.exists(path): print '---file exists--' else: print 'file not exists ',local_root os.mkdirs(local_root) return True except: return False def ftpconnect(): ftp=FTP() ftp.set_debuglevel(2) ftp.connect(FTP_SERVER,21) ftp.login(USER,PWD) return ftp def downloadfile(): ftp = ftpconnect() print ftp.getwelcome() #顯示ftp伺服器歡迎資訊 li = ftp.nlst(FTP_PATH) print 'ftp: ',li for eachfile in li: localpath = 'e:'+eachfile print '-- open localpath --',localpath bufsize = 1024 isDir(localpath) fp = open(localpath,'wb+') ftp.retrbinary('RETR '+eachfile,fp.write,bufsize) fp.flush() ftp.set_debuglevel(0) #關閉除錯 fp.close() ftp.quit() #退出ftp伺服器 if __name__=="__main__": downloadfile()