python遠程下載

來源:互聯網
上載者:User

標籤:tpc   exists   pcl   username   python   model   style   inf   lis   

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遠程下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.