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 红包祝福语简短10字
18 周六早晨祝福语简短