zoukankan      html  css  js  c++  java
  • Python之FTP传输-乾颐堂

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    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' + ' ' % localFile)
                    return True
                else:
                    self.log_file.write('The localFile %s size is not same with the remoteFile' + ' ' % 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()

    www.qytang.com/
    http://www.qytang.com/cn/list/29/
    http://www.qytang.com/cn/list/28/446.htm
    http://www.qytang.com/cn/list/28/445.htm
    http://www.qytang.com/cn/list/28/444.htm
    http://www.qytang.com/cn/list/28/442.htm
    http://www.qytang.com/cn/list/28/440.htm
    http://www.qytang.com/cn/list/28/437.htm
    http://www.qytang.com/cn/list/28/435.htm
    http://www.qytang.com/cn/list/28/434.htm
    http://www.qytang.com/cn/list/28/433.htm
    http://www.qytang.com/cn/list/28/431.htm
    http://www.qytang.com/cn/list/28/328.htm
    http://www.qytang.com/cn/list/28/326.htm
    http://www.qytang.com/cn/list/28/429.htm

  • 相关阅读:
    强类型DataSet (2011-12-30 23:16:59)转载▼ 标签: 杂谈 分类: Asp.Net练习笔记 http://blog.sina.com.cn/s/blog_9d90c4140101214w.html
    整合91平台接入的ANE
    keychain不能导出p12证书的解决方法
    制作IOS ANE的基本流程
    SVN 提交失败 非LF行结束符
    ANE打包工具使用视频教程 -- 梦宇技术 @极客学院
    RSA算法原理
    IOS 之 NSBundle 使用
    iOS编程——Objective-C KVO/KVC机制
    视图横竖屏控制技巧
  • 原文地址:https://www.cnblogs.com/qytang/p/5584561.html
Copyright © 2011-2022 走看看