python远程下载

class SFTPModel:
    def __init__(self, host, user, password):
        t = paramiko.Transport((host, 22))
        t.connect(username=user, password=password)
        self.sftp = paramiko.SFTPClient.from_transport(t)  # use the style of t connection remote server

    def search_allfiles(self, path, basedir=None):
        """
        through sftp find all files in path
        :param sftp:
        :param path:
        :param basedir:
        :return:
        {'relative path ':'absolute path',}
        relative path for local
        absolute path for remote(source path)
        """
        if not basedir:
            basedir = os.path.basename(path)
        else:
            basedir = os.path.join(basedir, os.path.basename(path))
        filename_list_dic = {}
        mode = self.sftp.stat(path).st_mode
        if stat.S_ISDIR(mode):
            # is directory
            cur_filelist = self.sftp.listdir(path)
            for f in cur_filelist:
                filename_list_dic = dict(filename_list_dic.items() + self.search_allfiles(path + "/" + f, basedir).items())
        else:
            filename_list_dic[basedir] = path
        return filename_list_dic

    def download_file(self, from_file_path, to_local_path):
        """
        from from_file_path to to_local_path download this file
        :param from_file_path:
        :param to_local_path:
        :return:
        """
        if os.path.exists(to_local_path):
            standout_print("Info: %s exist" % to_local_path)
            return

        dir_local_path = os.path.dirname(to_local_path)
        if not os.path.exists(dir_local_path):
            os.makedirs(dir_local_path)

        to_local_path = to_local_path.replace("\\", "/")

        self.sftp.get(from_file_path, to_local_path)
        standout_print("download file %s from %s finish." % (to_local_path, from_file_path))

    def download_directory(self, from_file_directory, to_local_directory):

        if not os.path.exists(to_local_directory):
            sys.stderr("Error: local directory of compile path is no exist")
            sys.exit(-1)

        base_dir = os.path.basename(from_file_directory)
        file_list_dir = self.search_allfiles(from_file_directory)
        for relative_path in file_list_dir.keys():
            to_local_path = os.path.join(to_local_directory, relative_path)
            from_file = file_list_dir[relative_path]
            self.download_file(from_file, to_local_path)

  

posted @ 2017-09-25 20:29  百变小超  阅读(276)  评论(0编辑  收藏  举报