Python FTP transfer

高洛峰
Release: 2016-10-18 10:34:56
Original
1302 people have browsed it

Accessing FTP is nothing more than two things: upload and download. Recently, I need to download a large number of files from ftp in the project, and then I tried to experiment with my own ftp operation class, as follows (PS: There is something wrong with this paragraph, please do not copy and use it. You can refer to it to test your own ftp class)

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()
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template