编写Python脚本来实现最简单的FTP下载的教程

 更新时间:2015年05月04日 10:58:28   投稿:goldensun  
这篇文章主要介绍了编写Python脚本来实现最简单的FTP下载的教程,主要用到了ftplib模块,无图形界面...需要的朋友可以参考下

访问FTP,无非两件事情:upload和download,最近在项目中需要从ftp下载大量文件,然后我就试着去实验自己的ftp操作类,如下(PS:此段有问题,别复制使用,可以参考去试验自己的ftp类!)

import os
from ftplib import FTP
 
class FTPSync():
  def __init__(self, host, usr, psw, log_file):
    self.host = host
    self.usr = usr
    self.psw = psw
    self.log_file = log_file
   
  def __ConnectServer(self):
    try:
      self.ftp = FTP(self.host)
      self.ftp.login(self.usr, self.psw)
      self.ftp.set_pasv(False)
      return True
    except Exception:
      return False
   
  def __CloseServer(self):
    try:
      self.ftp.quit()
      return True
    except Exception:
      return False
   
  def __CheckSizeEqual(self, remoteFile, localFile):
    try:
      remoteFileSize = self.ftp.size(remoteFile)
      localFileSize = os.path.getsize(localFile)
      if localFileSize == remoteFileSize:
        return True
      else:
        return False
    except Exception:
      return None
     
  def __DownloadFile(self, remoteFile, localFile):
    try:
      self.ftp.cwd(os.path.dirname(remoteFile))
      f = open(localFile, 'wb')
      remoteFileName = 'RETR ' + os.path.basename(remoteFile)
      self.ftp.retrbinary(remoteFileName, f.write)
       
      if self.__CheckSizeEqual(remoteFile, localFile):
        self.log_file.write('The File is downloaded successfully to %s' + '\n' % localFile)
        return True
      else:
        self.log_file.write('The localFile %s size is not same with the remoteFile' + '\n' % localFile)
        return False
    except Exception:
      return False
   
  def __DownloadFolder(self, remoteFolder, localFolder):
    try:
      fileList = []
      self.ftp.retrlines('NLST', fileList.append)
      for remoteFile in fileList:
        localFile = os.path.join(localFolder, remoteFile)
        return self.__DownloadFile(remoteFile, localFile)
    except Exception:
      return False
   
  def SyncFromFTP(self, remoteFolder, localFolder):
    self.__DownloadFolder(remoteFolder, localFolder)
    self.log_file.close()
    self.__CloseServer()

相关文章

  • Python用Jira库来操作Jira

    Python用Jira库来操作Jira

    这篇文章主要介绍了Python如何用Jira库来操作Jira,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2020-12-12
  • python 使用matplotlib 实现从文件中读取x,y坐标的可视化方法

    python 使用matplotlib 实现从文件中读取x,y坐标的可视化方法

    今天小编就为大家分享一篇python 使用matplotlib 实现从文件中读取x,y坐标的可视化方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07
  • Python strip lstrip rstrip使用方法

    Python strip lstrip rstrip使用方法

    Python中的strip用于去除字符串的首位字符,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符。这三个函数都可传入一个参数,指定要去除的首尾字符。
    2008-09-09
  • 使用beaker让Facebook的Bottle框架支持session功能

    使用beaker让Facebook的Bottle框架支持session功能

    这篇文章主要介绍了使用beaker让Facebook的Bottle框架支持session功能,session在Python的Django等框架中内置但在Bottle中并没有被集成,需要的朋友可以参考下
    2015-04-04
  • python安装后的目录在哪里

    python安装后的目录在哪里

    在本篇内容里小编给各位分享的是关于python安装后的目录位置的知识点内容,需要的朋友们可以学习下。
    2020-06-06
  • CentOS 7下安装Python3.6 及遇到的问题小结

    CentOS 7下安装Python3.6 及遇到的问题小结

    这篇文章主要介绍了CentOS 7下安装Python3.6 及遇到的问题小结,需要的朋友可以参考下
    2018-11-11
  • Python pandas索引的设置和修改方法

    Python pandas索引的设置和修改方法

    索引的作用相当于图书的目录,可以根据目录中的页码快速找到所需的内容,下面这篇文章主要给大家介绍了关于Python pandas索引的设置和修改的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-06-06
  • Python绘制三维立体图详解与绘图填充方式

    Python绘制三维立体图详解与绘图填充方式

    这篇文章主要介绍了Python绘制三维立体图详解与绘图填充方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • python定时器使用示例分享

    python定时器使用示例分享

    这篇文章主要介绍了python定时器使用示例,需要的朋友可以参考下
    2014-02-02
  • Python实现博客快速备份的脚本分享

    Python实现博客快速备份的脚本分享

    本文针对博客园实现了一个自动备份脚本,可以快速将自己的文章备份成Markdown格式的独立文件,备份后的md文件可以直接放入到hexo博客中,感兴趣的可以了解一下
    2022-09-09

最新评论