FTP transmission in Python

Source: Internet
Author: User
Python FTP transfers access to FTP. there are only two things: upload and download. Recently, a large number of files need to be downloaded from ftp in the project. then I tried to experiment with my ftp operation class, as shown in the following figure (PS: This section has a problem. do not copy and use it. refer to test your ftp class !)

import osfrom 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()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.