python从ftp服务器下载文件的示例

  介绍

这篇文章给大家分享的是有关python从ftp服务器下载文件的示例的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

代码之余,将代码过程重要的一些代码段备份一下,如下的代码内容是关于python从ftp服务器下载文件的的代码,希望能对小伙伴有用途。

#=utf - 8编码   & # 39;& # 39;& # 39;   ,ftp自动下载,自动上传脚本,可以递归目录操作   & # 39;& # 39;& # 39;      得到ftplib  import  FTP   import 操作系统、sys字符串,日期时间,时间   import 套接字      class  MYFTP:   ,def  __init__(自我,,hostaddr,,用户名,密码,remotedir,,端口=21):=,self.hostaddr  hostaddr=,self.username 用户名=,self.password 密码=,self.remotedir  remotedir   ,self.port =,端口   ,self.ftp =, FTP ()=,self.file_list  []   ,# self.ftp.set_debuglevel (2)   ,def  __del__(自我):   ,self.ftp.close ()   ,# self.ftp.set_debuglevel (0)   ,def 登录(自我):=,ftp  self.ftp   试题:大敌;   timeout =, 300   ,socket.setdefaulttimeout(超时)   ,ftp.set_pasv(真正的)   ,print  u # 39;开始连接到,% & # 39;,% (self.hostaddr)   ,ftp.connect (self.hostaddr, self.port)   ,print  u # 39;成功连接到,% & # 39;,% (self.hostaddr)   ,print  u # 39;开始登录到,% & # 39;,% (self.hostaddr)   ,ftp.login (self.username, self.password)   ,print  u # 39;成功登录到,% & # 39;,% (self.hostaddr)   ,debug_print (ftp.getwelcome ())   ,except 例外:   ,print  u # 39;连接或登录失败& # 39;   ,试一试:   ,ftp.cwd (self.remotedir)   ,除了(异常):   ,print  u # 39;切换目录失败& # 39;      ,def  is_same_size (localfile,自我,还以为;remotefile):   ,试一试:=,,remotefile_size  self.ftp.size (remotefile)   ,除了:   remotefile_size =, 1   ,试一试:=,,localfile_size  os.path.getsize (localfile)   ,除了:   localfile_size =, 1   ,debug_print (& # 39; localfile_size: % d  remotefile_size: % d # 39;, % (remotefile_size localfile_size也),)   ,if  remotefile_size ==, localfile_size:   return  1   ,其他的:   return  0   ,def  download_file (localfile,自我,还以为;remotefile):   ,if  self.is_same_size (localfile, remotefile):   ,debug_print (u # 39; % s 文件大小相同,无需下载& # 39;,% localfile)   ,返回   ,其他的:   ,debug_print (u # 39;在在在在在在在在在在在在下载文件,% s …,…& # 39;, % localfile)   ,#返回=,file_handler 开放(localfile, & # 39; wb # 39;)   ,self.ftp.retrbinary (u # 39; RETR  % & # 39; % (remotefile), file_handler.write)   ,file_handler.close ()      ,def  download_files(自我,,localdir=& # 39;。/& # 39;,, remotedir=& # 39;。/& # 39;):   ,试一试:   ,self.ftp.cwd (remotedir)   ,除了:   ,debug_print (u # 39;目录% s不存在,继续……& # 39;,% remotedir)   ,返回   ,if  not  os.path.isdir (localdir):   ,os.makedirs (localdir)   ,debug_print (u # 39;切换至目录,% & # 39;,% self.ftp.pwd ())=,self.file_list  []   ,self.ftp.dir (self.get_file_list)=,remotenames  self.file_list   ,#打印(remotenames)   ,#返回   ,for  item  remotenames拷贝:   ,filetype [0]=,项=,filename 项目[1]=,,local  os.path.join (localdir,文件名)==,if  filetype  & # 39; d # 39;:   ,self.download_files(本地,文件名)==,elif  filetype  & # 39; & # 39;   ,self.download_file(本地,文件名)   ,self.ftp.cwd (& # 39; . . & # 39;)   ,debug_print (u # 39;返回上层目录,% & # 39;,% self.ftp.pwd ())   ,def  upload_file (localfile,自我,还以为;remotefile):   ,if  not  os.path.isfile (localfile):   ,返回   ,if  self.is_same_size (localfile, remotefile):   ,debug_print (u # 39;跳过[相等):,% & # 39;,% localfile)   ,返回=,file_handler 开放(localfile, & # 39; rb # 39;)   ,self.ftp.storbinary (& # 39; STOR  % & # 39;, % remotefile,, $ file_handle)   ,file_handler.close ()   ,debug_print (u # 39;已传送:,% & # 39;,% localfile)   ,def  upload_files(自我,,localdir=& # 39;。/& # 39;,, remotedir =, & # 39;。/& # 39;):   ,if  not  os.path.isdir (localdir):   ,返回=,,localnames  os.listdir (localdir)   ,self.ftp.cwd (remotedir)   ,for  item  localname拷贝:=,,src  os.path.join (localdir,,项目)   ,if  os.path.isdir (src):   ,试一试:   self.ftp.mkd才能(项)   ,除了:   debug_print才能(u # 39;目录已存在,% & # 39;,%项)   ,self.upload_files (src,项)   ,其他的:   ,self.upload_file (src,项)   ,self.ftp.cwd (& # 39; . . & # 39;)      ,def  get_file_list(自我,,行):=,ret_arr  []=,,file_arr  self.get_filename(线)   ,if  file_arr [1], not 拷贝(& # 39;强生# 39;,,& # 39;. . & # 39;):   ,self.file_list.append (file_arr)   ,   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

python从ftp服务器下载文件的示例