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)

  

时间: 2024-08-05 07:42:23

python远程下载的相关文章

Python远程登录Linux操作系统,执行命令、创建目录、上传及下载文件

主要是创建文件: #! /bin/bash # -*- coding: utf-8 -*- import paramiko import os ssh = paramiko.SSHClient() key = paramiko.AutoAddPolicy() ssh.set_missing_host_key_policy(key) pkey = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa') paramiko.util.log

python将下载地址转换成迅雷和qq旋风的下载地址

迅雷和qq旋风下载,有加速和离线功能,很方面,我是在网上看到的原始地址和迅雷地址,qq旋风地址的转化原理,然后用python+pyqt写了一个客户端 原理: 迅雷: 迅雷下载地址="thunder://"+Base64编码("AA"+"真实地址"+"ZZ") QQ旋风: qqdl="qqdl://"+Base64编码("真实地址") import re import base64 fro

Python 远程调用脚本之 RPC

最近有个监控需求,需要远程执行集群每个节点上的脚本,并获取脚本执行结果,为了安全起见不需要账号密码登陆主机,要求只需要调用远程脚本模块的方法就能实现. 总结下python进行远程调用脚本方法: 登陆主机执行脚本,python模块支持如 pssh.pexpect.paramiko 以远程方法调用(不需要登陆主机),python模块 rpyc,支持分布式 socket 方式,稍显复杂,需要熟悉网络协议,起点比较高 rpyc支持远程调用.分布式计算,以较少代码量实现需复杂socket编程,本文主要介绍

Python 远程调用MetaSploit

(1)安装Python的msgpack类库,MSF官方文档中的数据序列化标准就是参照msgpack. [email protected]:~# apt-get install python-setuptools [email protected]:~# easy_install msgpack-python (2)创建createdb_sql.txt: create database msf; create user msf with password 'msf123'; grant all p

File / Directory 文件的操作及远程下载

//文件远程下载 WebClient client = new WebClient();    Uri uri = new Uri(url);    client.DownloadFile(Uri uri,String filename); //文件一定得存在不然会报错所以在删除文件时先判断这个文件是否存在 File.Exists(filePath) File.Delete(filePath); //File对文件的操作 File.Move(sourceFile,destFile);//文件的移

JAVA远程下载文件并读取实例方法

简单的文件下载后读取显示,该方法可返回内容的结果集.一般适用于文本文档的下载,以供学习交流. ** * 远程下载文件并读取返回p * @param filePath 文件网络地址 如http://www.baidu.com/1.txt * @return String */ public String DownAndReadFile(String filePath){ String date = new SimpleDateFormat("yyyy-MM-dd").format(new

PHP CURL实现远程下载文件到本地

<?php //$result=httpcopy('http://www.phpernote.com/image/logo.gif'); echo '<pre>';print_r($result); function httpcopy($url,$file='',$timeout=60){ $file=empty($file)?pathinfo($url,PATHINFO_BASENAME):$file; $dir=pathinfo($file,PATHINFO_DIRNAME); !i

树莓派:迅雷远程下载

极好的教程,步骤清晰,照着一步一步来轻松成功,转自:http://mkitby.com/2016/04/06/raspberry-pi-nas-remote-download-xunlei/              谢谢原作者! 第一步,去迅雷网站下载固件.http://luyou.xunlei.com/forum-51-1.html 找到最近的下载就可以了,注意Raspberry Pi 使用的是Xware版本号_armel_v5te_glibc.zip.我是用的是第九周的固件“Xware1.

使用python远程登录

最近要使用python做一个在web上管理交换机的程序,需要远程登录,就查了点资料,由于还没有搞到交换机,就先用自己的机器测试一下. 首先python的标准库中包含telnet,用起来也很方便,查看一下文档写了个小程序: [python] view plaincopy #!/usr/bin/env python #coding=utf-8 import telnetlib host = "127.0.0.1" userName = 'root' password = '123456'