python2.7实现FTP文件下载功能
本文实例为大家分享了python实现FTP文件下载功能的具体代码,供大家参考,具体内容如下
代码:
#-*-coding:utf-8-*- importos importtime fromftplibimportFTP #服务器地址 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())) printDATE defisDir(filename): try: path=filename; path.replace('/','\\') ifos.path.exists(path): print'---fileexists--' else: print'filenotexists',local_root os.mkdirs(local_root) returnTrue except: returnFalse defftpconnect(): ftp=FTP() ftp.set_debuglevel(2) ftp.connect(FTP_SERVER,21) ftp.login(USER,PWD) returnftp defdownloadfile(): ftp=ftpconnect() printftp.getwelcome()#显示ftp服务器欢迎信息 li=ftp.nlst(FTP_PATH) print'ftp:',li foreachfileinli: localpath='e:'+eachfile print'--openlocalpath--',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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。