微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python FTP传输的简单示例

对python这个高级语言感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!

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


# @param Python之FTP传输
# @author 编程之家 jb51.cc|jb51.cc 

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,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,localFolder):
        self.__DownloadFolder(remoteFolder,localFolder)
        self.log_file.close()
        self.__CloseServer()

# End www.jb51.cc

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐