python use ftp to download/updown

来源:互联网 发布:北京耳机实体店 知乎 编辑:程序博客网 时间:2024/06/15 06:50

支持目录下载/上传

vsftpd 需要打开root用户

下载: 支持下载目录下所有的文件(包含子文件夹里面的文件)

上传:支持上传目录下所有的文件(包含子文件夹里面的文件)

主要的思路就是遍历配置文件夹下的所有文件

配置文件:

[getfile]
server = 192.168.81.128
port = 21
username = root
password = root
mode = 2
localpath = /disk1_123/uploadtest3/uploadtest4/uploadtest5/6
bufsize = 1024
remotepath = /var/ssos/trunk/res/exam.res/bin/gen_ftr_fa/gen_ftr_fa.bin


python 2.7

python 代码:

'''
'''
import ConfigParser
import os
import ftplib

class FileTransfer():
    CFGPATH = "../../cfg/eis.cfg"
    GETFILE = 'getfile'
    
    def __init__(self):
        '''
        '''
        self.cf = ConfigParser.ConfigParser()
        self._cfg = os.path.abspath(self.CFGPATH)
        self.cf.read(self._cfg)
        self.ftp = ftplib.FTP()
    
    def checkdir(self, cpath):
        '''
        check file exist
        '''
        rpath = ''
        lpath = ''
        mode = self.cf.get(self.GETFILE, 'mode')
        if(int(mode) == 1):
#           localpath = self.cf.get(self.GETFILE, 'localpath')
             
            if cpath[-1] == '/':
                path = cpath.rstrip('/').lstrip('/').split('/')
                for p in path:
                    lpath += '/' + p
                    try:
                        self.ftp.cwd(lpath)
                    except:
                        try:
                            self.ftp.mkd(lpath)
                        except ftplib.error_perm, e:
                            print e
                        
            else:
                path = cpath.lstrip('/').split('/')
                path.pop(-1)
                for p in path:
                    lpath += '/' + p
                    try:
                        self.ftp.cwd(lpath)
                    except:
                        try:
                            self.ftp.mkd(lpath)
                        except ftplib.error_perm, e:
                            print e
        if(int(mode) == 2):
#             remotepath = self.cf.get(self.GETFILE, 'remotepath')
            if cpath[-1] == '/':
                path = cpath.rstrip('/').lstrip('/').split('/')
                for p in path:
                    rpath += '/' + p
                    if not os.path.exists(rpath):
                        try:
                            os.mkdir(rpath)
                        except Exception, e:
                            print e
            else:
                path = cpath.lstrip('/').split('/')
                path.pop(-1)
                for p in path:
                    rpath += '/' + p
                    if not os.path.exists(rpath):
                        try:
                            os.mkdir(rpath)
                        except Exception, e:
                            print e

    def create_folder(self, lpath):
        '''
        get all file in ftp
        '''
        mstatus = ''
        folder = []
        status = True
        self.log('now in create folder')
        self.log(lpath)
        try:
            self.ftp.cwd(lpath)
            self.log('folder')
            remotefiles = self.ftp.nlst()
                    
            for p in remotefiles:
                path = lpath + p
                try:
                    self.ftp.cwd(path)
                    folder.append(path + '/')        
                except:
                    folder.append(path)
        except:
            folder.append(lpath)

        return folder
    
    def get_filepath(self, path):
        '''
        '''
        bufsize = self.open_ftp()
        self.log(path)
        file = []
        folder = self.create_folder(path)
        for flog in folder:
            self.log(flog)
        for f in folder:
            while f[-1] == '/':
                tmp = self.create_folder(f)
                if len(tmp):
                    for t in tmp:
                        folder.append(t)
                        f = t
                else:
                    f = 'empty'
        folder = list(set(folder))
        for fod in folder:
            if(fod[-1] != '/'):
                file.append(fod)
        for fflog in file:
            self.log(fflog)
        self.close_ftp()
        return file
        
    def open_ftp(self):
        '''
        connect and login ftp
        '''
        server = self.cf.get(self.GETFILE, "server")
        port = self.cf.get(self.GETFILE, "port")
        user = self.cf.get(self.GETFILE, "username")
        passwd = self.cf.get(self.GETFILE, 'password')    
        bufsize = int(self.cf.get(self.GETFILE, 'bufsize'))
        
        self.ftp.set_debuglevel(0)

        try:
            self.ftp.connect(server, port)
        except Exception, e:
            print e
            return False
        
        try:
            self.ftp.login(user,passwd)
        except ftplib.error_perm, e:  
            self.ftp.quit()
            print e
            return False
        
        return bufsize
    
    def close_ftp(self):
        '''
        close ftp
        '''
        self.ftp.quit()
     
    def file_upload(self):
        '''
        upload file by ftp
        '''
        path = self.cf.get(self.GETFILE, 'localpath')
        bufsize = self.open_ftp()
        self.checkdir(self.cf.get(self.GETFILE, 'localpath'))
        if bufsize is False:
            return
        if path[-1] == '/':
               
            try:
                localfiles = os.listdir(path)
            except OSError, e:
                print e
                return
                
            for file in localfiles:
                localpath = os.path.join(path, file)
                if os.path.isfile(localpath):
                    file_handle = open(localpath,'rb')
                    
                    try:
                        self.ftp.storbinary('STOR %s' % (localpath), file_handle, bufsize)
                    except ftplib.error_perm, e:
                        print e
                    file_handle.close()
        else:
            file_handle = open(path,'rb')
            try:
                self.ftp.storbinary('STOR %s' % (path), file_handle, bufsize)
            except ftplib.error_perm, e:
                print e
            file_handle.close()
        
        self.close_ftp()
        print 'ftp upload successfully'
        
    def file_download(self):
        '''
        download file by ftp
        '''
        
        self.checkdir(self.cf.get(self.GETFILE, 'remotepath'))
        path = self.cf.get(self.GETFILE, 'remotepath')
        bufsize = self.open_ftp()

        if bufsize is False:
            return

        if path[-1] == '/':
            
            try:
                self.ftp.cwd(path)
                remotefiles = self.ftp.nlst()
            except Exception, e:
                print e
                return
            
            for file in remotefiles:
                remotepath = os.path.join(path, file)
                file_handle = open(remotepath,'wb')
                try:
                    self.ftp.retrbinary('RETR %s' % (remotepath), file_handle.write, bufsize)
                except ftplib.error_perm, e:
                    print e
                file_handle.close()
        else:
            try:         
                file_handle = open(path,'wb')
            except Exception, e:
                print e
                return
            
            try:
                self.ftp.retrbinary('RETR %s' % (path), file_handle.write, bufsize)
            except ftplib.error_perm, e:
                print e
            file_handle.close()  
        self.close_ftp()

        print 'ftp download successfully'
    
    def log(self, c):
        '''
        '''
        log = '/var/log/celery.log'
        f=open(log,'a')
        f.write(time.strftime("%m/%d/%Y %H:%M") + ' ')
        f.write(c + '\n')
        f.close()
    
    def run(self):
        '''
        '''
        
        modes = self.cf.get(self.GETFILE, 'mode')
        if int(modes) == 2:
            self.file_download()
             
        elif int(modes) == 1:
            self.file_upload()
        else:
            print 'no this mode'

1 0