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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志