python 3 ftpリモートバッチ処理ツール

4907 ワード

直接上コード:ftpFun.py
#coding=utf-8

import ftplib
import os
import os.path

class ftpInterface:
    
    def __init__(self, host = '', port = 21, user = '', pwd = '', timeout = 5):
        self.host = host
        self.port = port
        self.user = user
        self.pwd = pwd
        self.timeout = timeout
        self.filesParentList = []
        self.connect()
        self.ftp.encoding = 'GB18030'
        #ls = self.ftp.dir()
        #print(ls)
    
    def setlocalPath(self, ftpEditPath):    
        #self.ftpPath = ftpEditPath
        #self.ftpPathSetting('')
        workPath = self.ftp.pwd()
        self.ftp.cwd(workPath)
    
    def connect(self):
        try:
            self.ftp = ftplib.FTP()
            self.ftp.connect(self.host, self.port, self.timeout)
        except Exception as connectEx:
            print(connectEx)
        self.login()
            
    def close(self):
        try:
            self.ftp.quit()
        except Exception as closeEx:
            print(closeEx)
    
    def login(self):
        try:
            self.ftp.login(self.user, self.pwd)
        except Exception as loginEx:
            print(loginEx)
        

    def ftpPathSetting(self, SettingPath):
        try:
            #substr = self.filesParentList[0]
            #pos = SettingPath.find(substr)
            #tmpPath = SettingPath[pos + len(substr):]
            #allPath = self.ftpPath + tmpPath
            #self.ftp.cwd(allPath)
            print(self.ftpPath)
            print(self.ftp.pwd())
            l = self.ftp.nlst()
            print(l)
            self.ftp.cwd(self.ftpPath)
            
        except Exception as ex:
            print(ex)
            
    
    def ftpUpLoadFile(self, folderPath):
        upFileMap = self.getFileNames(folderPath)
        if(not upFileMap):
            return -3
        try:
            isDirExist = False
            for fileKey in upFileMap.keys():
                '''find folderName == '1' pos '''
                filePath = self.getFtpFilePath(upFileMap[fileKey])
                if(filePath == ''):
                    continue
                else:
                    isDirExist = True
                ftpDir = os.path.dirname(filePath)
                print(ftpDir)
                self.createFtpDir(ftpDir)
                self.ftp.storbinary('STOR ' + filePath , open(upFileMap[fileKey], 'rb'))
            if(isDirExist == False):
                return -1
        except Exception as ex:
            print(ex)
            return -2
        self.close()
        return 0

    
    def getFileNames(self, folderPath):
        if(not os.path.isdir(folderPath)):
            return False
        fileNameMap = {}
        for parent, dirnames, filenames in os.walk(folderPath):
            self.filesParentList.append(parent)
            for filename in filenames:
                pathName = os.path.join(parent, filename)
                print(pathName)
                fileNameMap[filename] = pathName
        return fileNameMap        
        
    def ftpRemoveFile(self, filePath):
        try:
            path = self.getFtpFilePath(filePath)
            print(path)
            if(path == ''):
                return -1
            self.ftp.delete(path)
        except Exception as ex:
            return -2
            print(ex)
        self.close()
    
    def ftpRenameFile(self, beforName, afterName):
        try:
            path = self.getFtpFilePath(beforName)
            print(path)
            if(path == ''):
                return -1
            self.ftp.rename(beforName, afterName) #'/bbs/test.doc'
        except Exception as ex:
            return -2
            print(ex)
        self.close()
            
    def getFtpFilePath(self, srcPath):
        pos1 = srcPath.find('//voicePrompt//1')
        pos2 = srcPath.find('\\voicePrompt\\1')
        pos3 = srcPath.find('\voicePrompt\1')
        pos4 = srcPath.find('/voicePrompt/1')
        if(pos1 == -1 and pos2 == -1 and pos3 == -1 and pos4 == -1):
            return ''
        pos = 0
        posList = [pos1, pos2, pos3, pos4]
        for p in posList:
            if(p != -1):
                pos = p
                break
        filePath = srcPath[pos + len("/voicePrompt/1"):]
        return filePath 

    def createFtpDir(self, ftpPath):
        Paths = ftpPath.split('\\')
        print(Paths)
        tmpPath = ''
        for path in Paths:
            if(path == ''):
                continue
            tmpPath += '/' + path
            try:
                print(tmpPath)
                self.ftp.mkd(tmpPath)
            except Exception as ex:
                print(ex)
インタフェースの処理するコードを下ろすことができなくて、次の文章の中でアップロードします