欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

編寫Python腳本來實現(xiàn)最簡單的FTP下載的教程

 更新時間:2015年05月04日 10:58:28   投稿:goldensun  
這篇文章主要介紹了編寫Python腳本來實現(xiàn)最簡單的FTP下載的教程,主要用到了ftplib模塊,無圖形界面...需要的朋友可以參考下

訪問FTP,無非兩件事情:upload和download,最近在項目中需要從ftp下載大量文件,然后我就試著去實驗自己的ftp操作類,如下(PS:此段有問題,別復(fù)制使用,可以參考去試驗自己的ftp類?。?br />

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()

相關(guān)文章

  • Python用Jira庫來操作Jira

    Python用Jira庫來操作Jira

    這篇文章主要介紹了Python如何用Jira庫來操作Jira,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • python 使用matplotlib 實現(xiàn)從文件中讀取x,y坐標的可視化方法

    python 使用matplotlib 實現(xiàn)從文件中讀取x,y坐標的可視化方法

    今天小編就為大家分享一篇python 使用matplotlib 實現(xiàn)從文件中讀取x,y坐標的可視化方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Python strip lstrip rstrip使用方法

    Python strip lstrip rstrip使用方法

    Python中的strip用于去除字符串的首位字符,同理,lstrip用于去除左邊的字符,rstrip用于去除右邊的字符。這三個函數(shù)都可傳入一個參數(shù),指定要去除的首尾字符。
    2008-09-09
  • 使用beaker讓Facebook的Bottle框架支持session功能

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

    這篇文章主要介紹了使用beaker讓Facebook的Bottle框架支持session功能,session在Python的Django等框架中內(nèi)置但在Bottle中并沒有被集成,需要的朋友可以參考下
    2015-04-04
  • python安裝后的目錄在哪里

    python安裝后的目錄在哪里

    在本篇內(nèi)容里小編給各位分享的是關(guān)于python安裝后的目錄位置的知識點內(nèi)容,需要的朋友們可以學(xué)習(xí)下。
    2020-06-06
  • CentOS 7下安裝Python3.6 及遇到的問題小結(jié)

    CentOS 7下安裝Python3.6 及遇到的問題小結(jié)

    這篇文章主要介紹了CentOS 7下安裝Python3.6 及遇到的問題小結(jié),需要的朋友可以參考下
    2018-11-11
  • Python?pandas索引的設(shè)置和修改方法

    Python?pandas索引的設(shè)置和修改方法

    索引的作用相當(dāng)于圖書的目錄,可以根據(jù)目錄中的頁碼快速找到所需的內(nèi)容,下面這篇文章主要給大家介紹了關(guān)于Python?pandas索引的設(shè)置和修改的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-06-06
  • Python繪制三維立體圖詳解與繪圖填充方式

    Python繪制三維立體圖詳解與繪圖填充方式

    這篇文章主要介紹了Python繪制三維立體圖詳解與繪圖填充方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • python定時器使用示例分享

    python定時器使用示例分享

    這篇文章主要介紹了python定時器使用示例,需要的朋友可以參考下
    2014-02-02
  • Python實現(xiàn)博客快速備份的腳本分享

    Python實現(xiàn)博客快速備份的腳本分享

    本文針對博客園實現(xiàn)了一個自動備份腳本,可以快速將自己的文章備份成Markdown格式的獨立文件,備份后的md文件可以直接放入到hexo博客中,感興趣的可以了解一下
    2022-09-09

最新評論