コード例 #1
0
ファイル: GN_Arcal_FTP.py プロジェクト: jmramon/python-int
def uploadFilesToFTP(ftpURL, ftpUser, ftpPassword, ftpPath, localPath,
                     filematch, historyBackupPath):

    ftps = FTP_TLS(ftpURL)
    ftps.set_debuglevel(1)
    ftps.set_pasv(False)
    ftps.connect(port=21, timeout=80)
    ftps.login(ftpUser, ftpPassword)
    ftps.prot_p()
    ftps.ccc()
    try:
        ftps.cwd(ftpPath)
    except Exception:
        ftps.mkd(ftpPath)

    for (localPathDir, _, files) in os.walk(localPath):
        newdir = ftpPath
        try:
            ftps.cwd(newdir)
        except Exception:
            ftps.mkd(newdir)

        LOGGER.info("filematch=" + filematch)

        for f in fnmatch.filter(files, filematch):
            fileLocalPath = os.path.join(localPathDir, f)
            file = open(fileLocalPath, 'rb')
            ftps.storbinary('STOR ' + f, file, blocksize=8192)
            file.close()
            LOGGER.info("Fichero transferido #:# " + fileLocalPath)
            sleep(1)
            now = datetime.datetime.now()
            historyBackupPathYear = os.path.join(historyBackupPath,
                                                 str(now.year))

            try:
                os.stat(historyBackupPathYear)
            except:
                os.mkdir(historyBackupPathYear)

            moveFilesUUID(fileLocalPath, historyBackupPathYear)

    ftps.close()