zoukankan      html  css  js  c++  java
  • 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)
    

      

  • 相关阅读:
    SQL Server 重新组织生成索引
    atitit.软件设计模式大的总结attialx总结
    Linux防火墙限制指定port仅仅能由指定IP訪问
    android TextView里边实现图文混配效果
    Codeforces Round #270
    HTML5中x-webkit-speech语音输入功能
    oracle11g中SQL优化(SQL TUNING)新特性之SQL Plan Management(SPM)
    Android-HttpURLConnection自己主动管理cookie
    iOS UI01_UIView
    Okio简化处理I/O操作原理
  • 原文地址:https://www.cnblogs.com/dasheng-maritime/p/7593798.html
Copyright © 2011-2022 走看看