python自动化脚本ftp应用

#!/usr/bin/python
# -*- coding:utf-8 -*-
from ftplib import FTP
import os


CONST_HOST = '127.0.0.1'
CONST_PORT = 8200
CONST_USERNAME = 'admin'
CONST_PWD = 'password'


class FtpTree:
    def __init__(self,verbose = False):
        self.verbose = verbose
        try:
            self.ftp = FTP()
            self.ftp.connect(CONST_HOST,CONST_PORT)
            self.ftp.login(CONST_USERNAME,CONST_PWD)
        except:
            if self.verbose:
                print('ftp connect failed!')
                exit(0)
                
    def existflag(self,memberName):
        if memberName in self.ftp.nlst():
            return 1
        else:
            return 0
        
    def ftpmkdir(self,fileName):
        self.ftp.cwd(fileName)
        if self.verbose:
            print(self.ftp.dir())
    
    def ftpclose(self):
        self.ftp.quit()
    
    def deletefolder(self,folderName):
        self.ftpcwd(folderName)
        root_lists = self.ftp.nlst()
        for root_list in root_lists:
            try:
                self.ftp.delete(root_list)
            except:
                self.ftp.deletefolder(root_list)
        self.ftpcwd('..')
        self.ftprmd(folderName)
    
    def deletefile(self,fileName):
        self.ftp.delete(fileName)
    
    def deletermd(self,fileName):
        self.ftp.rmd(fileName)
    
    def uploadfile(self,filePath,fileName):
        if not self.existflag(fileName):
            fp = open(filePath,'rb')
            self.ftp.storbinary('STOR %s' % fileName,fp)
            fp.close()

    def uploadfolder(self,path):
        fileDirs = os.listdir(path)
for fileDir in fileDirs:
filePath = path + '/' + fileDir
if os.path.isfile(filePath):
self.uploadfile(filePath,fileDir)
if os.path.isdir(filePath):
try:
self.ftpmkdir(fileDir)
except:
pass
self.ftpcwd(fileDir)
self.uploadfolder(filePath)
self.ftpcwd('..')

def downloadfile(self,localPath,fileName):
localFilePath = localPath + '/' + fileName
if os.path.isdir(localPath) and self.existflag(fileName):
fp = open(localFilePath,'wb')
self.ftp.retrbinary('RETR %s' % fileName,fp.write)
fp.close()
else:
print('download failed!')
exit(0)

def downloadfolder(self,localPath):
if not os.path.isdir(localPath):
os.mkdir(localPath)
else:
print(localPath,' was already exist!')
listDirs = []
self.ftp.dir(listDirs.append)
for listDir in listDirs:
if '<DIR>' in listDir.split():
self.ftpcwd(listDir.split()[-1])
self.downloadfolder(localPath + '/' + listDir.split()[-1])
else:
self.downloadfolder(localPath, listDir.split()[-1])
self.ftpcwd('..')

#self test
if __name__ == '__main__':
ftpclass = FtpTree(verbose = True)
if ftpclass.existflag('svn_code'):
ftpclass.ftpcwd('svn_code')

# delete
if ftpclass.existflag('trunk_svn11135'):
ftpclass.deletefolder('trunk_svn11135')

# upload
fileName = 'trunk_svn11136'
folderPath = 'd:/svn' + '/' + fileName
if os.path.exists(folderPath):
ftpclass.ftpmkdir(fileName)
ftpclass.ftpcwd(fileName)
ftpclass.uploadfolder(folderath)
else:
print('upload file not exist!')

# download
downloadDir = 'd:/svn'
downloadFile = 'trunk_svn11303'
if ftpclass.existflag(downloadFile):
ftpclass.ftpcwd(downloadFile)
ftpclass.downloadfolder(downloadDir + '/' + downloadFile)
else:
print(downloadFile, ' not exist!')
exit(0)

ftpclass.ftpclose()

你可能感兴趣的:(python自动化脚本ftp应用)