Python 基于Python实现的ssh兼sftp客户端(下)
基于Python实现的ssh兼sftp客户端
by:授客 QQ:1033553122
otherTools.py
#!/usr/bin/env/ python
#
-*- coding:utf-8 -*-
__author__
=
'laifuyu'
import
os
import
subprocess
class
OtherTools:
def
__init__(self):
self.filepath_list
= []
#
批量创建目录
def
mkdirs_once_many(self,
path):
path
= os.path.normpath(path) #
去掉路径最右侧的 \\
、/
path
= path.replace('\\',
'/')
# 将所有的\\转为/,避免出现转义字符串
head,
tail = os.path.split(path)
new_dir_path
=
'' #
反转后的目录路径
root
=
'' #根目录
if
not
os.path.isdir(path)
and
os.path.isfile(path): #
如果path指向的是文件,则继续分解文件所在目录
head,
tail = os.path.split(head)
if
tail ==
'':
return
while
tail:
new_dir_path
= new_dir_path + tail +
'/'
head,
tail = os.path.split(head)
root
= head
else:
new_dir_path
= root + new_dir_path
#
批量创建目录
new_dir_path
= os.path.normpath(new_dir_path)
head,
tail = os.path.split(new_dir_path)
temp
=
''
while
tail:
temp
= temp +
'/'
+ tail
dir_path
= root + temp
if
not
os.path.isdir(dir_path):
os.mkdir(dir_path)
head,
tail = os.path.split(head)
# 测试
ssh_client
= MySSHClient()
ssh_client.connect(hostname='192.168.1.102',
port=22,
username='root',password='huozhe')
ssh_client.exec_command('ls
-l')
ssh_client.download_file('/root/dirForDownload/file',
'./test1.txt')
ssh_client.download_file('/root/dirForDownload/file',
'.\test2.txt')
ssh_client.download_file('/root/dirForDownload/file',
'd:\\test3.txt')
ssh_client.download_file('/root/dirForDownload/file',
'd:\test4.txt')
ssh_client.download_file('/root/dirForDownload/file',
'd:\mytest4.txt')
ssh_client.download_file('/root/dirForDownload/file',
'd:/test5.txt')
ssh_client.download_file('/root/dirForDownload/file',
'd:\dir1\dir2\test6.txt')
ssh_client.upload_file('./test1.txt','/root/test1.txt'
)
ssh_client.upload_file('d:\mytest4.txt','/root/mytestfile.txt'
)
ssh_client.upload_file('d:\dir1\dir2\test6.txt','./test6.txt'
)
ssh_client.close()
运行结果:

注意事项
# 1. 下载文件
#
1) 不支持目录级的下载,即只能下载指定的单个非目录文件
#
2) 本地目标文件路径只支持文件路径,不支持目录(比如 localpath='d:\\'),目标文件所在的上级路径可以不存在(但路径必须位于分区下)
#
比如欲下载到本地路径:d:\dir1\dir2\test.txt, d:\dir1\dir2\可以不存在
#
3) 本地目标文件支持相对路径,比如./text.txt,远程目标文件仅支持绝对路径
#
2. 上传文件
#
1) 不支持目录级的上传,只能上传指定的单个文件
#
2) 远程目标文件所在的上级路径必须存在,比如remotepath='/root/dir1/tarfile' ,其中/root/dir1必须存在
#
3) 远程目标文件、本地文件路径都支持相对路径,比如./text.txt
#
3. 重复下载文件、上传文件,会自动覆盖已经下载的文件、已上传的文件
参考文档:
http://docs.paramiko.org/en/2.4/api/channel.html
http://docs.paramiko.org/en/2.4/api/sftp.html#paramiko.sftp_client.SFTPClient
Python 基于Python实现的ssh兼sftp客户端(下)的更多相关文章
- Python 基于Python实现的ssh兼sftp客户端(上)
基于Python实现的ssh兼sftp客户端 by:授客 QQ:1033553122 实现功能 实现ssh客户端兼ftp客户端:实现远程连接,执行linux命令,上传下载文件 测试环境 Win7 ...
- Python 基于Python及zookeeper实现简单分布式任务调度系统设计思路及核心代码实现
基于Python及zookeeper实现简单分布式任务调度系统设计思路及核心代码实现 by:授客 QQ:1033553122 测试环境 功能需求 实现思路 代码实践(关键技术点实现) 代码模块组织 ...
- Python 基于python操纵zookeeper介绍
基于python操纵zookeeper介绍 by:授客 QQ:1033553122 测试环境 Win7 64位 Python 3.3.4 kazoo-2.6.1-py2.py3-none-any.w ...
- Python 基于Python结合pykafka实现kafka生产及消费速率&主题分区偏移实时监控
基于Python结合pykafka实现kafka生产及消费速率&主题分区偏移实时监控 By: 授客 QQ:1033553122 1.测试环境 python 3.4 zookeeper- ...
- Python 基于Python从mysql表读取千万数据实践
基于Python 从mysql表读取千万数据实践 by:授客 QQ:1033553122 场景: 有以下两个表,两者都有一个表字段,名为waybill_no,我们需要从tl_waybill_b ...
- Python基于Python实现批量上传文件或目录到不同的Linux服务器
基于Python实现批量上传文件或目录到不同的Linux服务器 by:授客 QQ:1033553122 实现功能 1 测试环境 1 使用方法 1 1. 编辑配置文件conf/rootpath_fo ...
- Python 基于python编写一些算法程序等
基于python编写一些算法程序等 by:授客 QQ:1033553122 QQ群:7156436 没特意去研究,只是这对群友在QQ群里(7156436)提出的一些小程序实现.编程题,算法.问题等,本 ...
- Python 基于python+mysql浅谈redis缓存设计与数据库关联数据处理
基于python+mysql浅谈redis缓存设计与数据库关联数据处理 by:授客 QQ:1033553122 测试环境 redis-3.0.7 CentOS 6.5-x86_64 python 3 ...
- Python 基于python操纵redis入门介绍
基于python操纵redis入门介绍 by:授客 QQ:1033553122 测试环境 redis-3.0.7 CentOS 6.5-x86_64 python 3.3.2 基于Python操作R ...
随机推荐
- Anaconda下载地址
Anaconda installer archive:地址1: https://repo.continuum.io/archive/地址2:https://mirrors.tuna.tsinghua. ...
- Net Core平台灵活简单的日志记录框架NLog+Mysql组合初体验
Net Core平台灵活简单的日志记录框架NLog初体验 前几天分享的"[Net Core集成Exceptionless分布式日志功能以及全局异常过滤][https://www.cnblog ...
- 解决android sdk 运行出现 could not install *smartsocket* listener: cannot bind to 127.0.0.1:5037:的问题
ionic3项目,在添加android平台后,cordova run android 出现 以下问题: error: could not install *smartsocket* listener: ...
- 在SpringBoot中添加Redis
前言 在实际的开发中,会有这样的场景.有一个微服务需要提供一个查询的服务,但是需要查询的数据库表的数据量十分庞大,查询所需要的时间很长. 此时就可以考虑在项目中加入缓存. 引入依赖 在maven项目中 ...
- 【原创】Python第二章——行与缩进
Python的基本组成——逻辑行和缩进 a="我是一个物理行" a="""我是一个逻辑行 因为我一条语句便跨越了2个物理行""&q ...
- 如何在Eclipse上使用SVN,安装、提交、拉取代码、解决冲突等操作
https://blog.csdn.net/zhanlv/article/details/76504414 将SVN集成到Eclpise第一种方法第一种方法是通过zip文件在Eclipse中安装(本插 ...
- 使用 Synchronized 关键字
使用 Synchronized 关键字来解决并发问题是最简单的一种方式,我们只需要使用它修饰需要被并发处理的代码块.方法或字段属性,虚拟机自动为它加锁和释放锁,并将不能获得锁的线程阻塞在相应的阻塞队列 ...
- docsify网站文档工具用法总结
docsify , 网站文档动态生成工具,类似gitbook 特性 无需构建,写完文档直接发布 容易使用并且轻量 (~19kB gzipped) 智能的全文搜索 提供多套主题 丰富的 API 支持 E ...
- vue开发中,build时css打包报错
vue在打包的时候css报错,首先声明的这个并没有解决webpack本身的问题,本来这个也是一个警示,不会影响代码正常运行 这个是因为我在多个页面引入了同一个css文件,webpack打包时候对css ...
- JQuery官方学习资料(译):Attributes
Attributes 元素的属性可以为你的应用程序包含有用的信息,重要的是能够获取和设置它. .attr()方法 .attr()方法是可获取和可设置的,在设置状态下,.attr()可以 ...