python迅雷远程下载页面_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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值