python远程下载

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)

  

以上是关于python远程下载的主要内容,如果未能解决你的问题,请参考以下文章

PHP常用代码片段

Python 远程开发环境部署与调试

python 下载远程日志

常用python日期日志获取内容循环的代码片段

python远程下载

python 有用的Python代码片段