python通过sftp远程传输文件
python提供了一个第三方模块paramiko,通过这个模块可以实现两台机器之间的网络连接,sftp是paramiko的一个方法,使用sftp可以在两台机器之间互相传输
拷贝文件。然而paramiko的sftp只能拷贝文件,不能拷贝文件夹。要实现文件夹的拷贝,可以这样子思考:使用深度搜索遍历源目录,若是文件,直接拷贝到
目的目录;若是文件夹,则先在目的目录建立一个子目录,然后遍历子目录...使用递归函数,可以达到完全拷贝目录的目的。实现代码如下:
1.获取一个sftp句柄
import paramiko
#建立连接,获取sftp句柄def sftp_connect(username,password,host,port=22): client = None sftp = None try: client = paramiko.Transport((host,port)) except Exception as error: print error else: try: client.connect(username=username, password=password) except Exception as error: print error else: sftp = paramiko.SFTPClient.from_transport(client) return client,sftp#断开连接def disconnect(client): try: client.close() except Exception as error: print error2.从远程机器上下载文件(夹)到本地
import os
import sysfrom stat import S_ISDIR as isdirdef _check_local(local): if not os.path.exists(local): try: os.mkdir(local) except IOError as err: print errdef get(sftp,remote,local): #检查远程文件是否存在 try: result = sftp.stat(remote) except IOError as err: error = '[ERROR %s] %s: %s' %(err.errno,os.path.basename(os.path.normpath(remote)),err.strerror) print error else: #判断远程文件是否为目录 if isdir(result.st_mode): dirname = os.path.basename(os.path.normpath(remote)) local = os.path.join(local,dirname) _check_local(local) for file in sftp.listdir(remote): sub_remote = os.path.join(remote,file) sub_remote = sub_remote.replace('\\','/') get(sftp,sub_remote,local) else: #拷贝文件 if os.path.isdir(local): local = os.path.join(local,os.path.basename(remote)) try: sftp.get(remote,local) except IOError as err: print err else: print '[get]',local,'<==',remote3.从本地拷贝文件(夹)到远程机器
import os
def put(sftp,local,remote): #检查路径是否存在 def _is_exists(path,function): path = path.replace('\\','/') try: function(path) except Exception as error: return False else: return True #拷贝文件 def _copy(sftp,local,remote): #判断remote是否是目录 if _is_exists(remote,function=sftp.chdir): #是,获取local路径中的最后一个文件名拼接到remote中 filename = os.path.basename(os.path.normpath(local)) remote = os.path.join(remote,filename).replace('\\','/') #如果local为目录 if os.path.isdir(local): #在远程创建相应的目录 _is_exists(remote,function=sftp.mkdir) #遍历local for file in os.listdir(local): #取得file的全路径 localfile = os.path.join(local,file).replace('\\','/') #深度递归_copy() _copy(sftp=sftp,local=localfile,remote=remote) #如果local为文件 if os.path.isfile(local): try: sftp.put(local,remote) except Exception as error: print error print '[put]',local,'==>',remote,'FAILED' else: print '[put]',local,'==>',remote,'SUCCESSED' #检查local if not _is_exists(local,function=os.stat): print "'"+local+"': No such file or directory in local" return False #检查remote的父目录 remote_parent = os.path.dirname(os.path.normpath(remote)) if not _is_exists(remote_parent,function=sftp.chdir): print "'"+remote+"': No such file or directory in remote" return False #拷贝文件 _copy(sftp=sftp,local=local,remote=remote)python通过sftp远程传输文件的更多相关文章
- golang 远程传输文件
概述 之前有一篇介绍如何使用 golang 通过SSH协议来执行远程命令:golang 执行远程命令 同样,通过SSH协议也可以使用 golang 来远程传输文件. 除了 SSH 的库,为了传输文件, ...
- blfs(systemd版本)学习笔记-安装lrzsz软件包实现ssh远程传输文件到lfs系统
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 安装lrzsz软件包实现ssh远程传输文件到lfs系统 这个软件包在lfs系列的书中没有,这里是参照lrzsz官网的说明进行编译 ...
- Python+USB+Vnet+FTP传输文件开发记录
做一个Python+USB+Vnet+FTP传输文件开发记录
- Linux远程传输文件免密码
首先为什么Linux远程传输要免密码?手动使用scp命令传输每次都要输密码太过麻烦了. 开发中有一句话,能复制粘贴尽量不要手打. 运维中有一句话,能脚本化实现尽量不要手动执行. 远程传输文件免密码的目 ...
- Python不同电脑之间传输文件实现类似scp功能不输密码
SCP vs SFTP 通过paramiko还可以传输文件,如何通过paramiko在计算机之间传输文件,通过阅读官方文档,发现有如下两种方式: sftp = paramiko.SFTPClient. ...
- sftp远程传输脚本
该脚本主要是是批量修改文件名,然后传输文件到服务器#!/bin/bash #脚本名:sftp.sh while true do num=`cat test.txt|wc -l` ;a<=$num ...
- 远程传输文件命令:scp
1.概述 scp(secure copy)是一个基于 SSH 协议在网络之间进行安全传输的命令,其格式为“scp [参数] 本地文件 远程帐户@远程 IP 地址:远程目录”. 与第 2 章讲解的 cp ...
- SCP远程传输文件
今天想用SCP通过局域网传输文件到服务器,但却发生了下面这种事情: 上面描述 连接主机端口22被拒绝,失去连接 后发现因为没有指定端口,我服务器这边改了端口,所以根据自己情况改一下命令 scp -29 ...
- scp远程传输文件和ssh远程连接
ssh使用方法 如果从一台linux服务器通过ssh远程登录到另一台Linux机器, 这种情况通常会在多台服务器的时候用到. 如用root帐号连接一个IP为192.168.1.102的机器,输入:“ ...
随机推荐
- eclipse回退到上个版本
在 team->show in history 中 选择版本,执行Revert Commit,然后push 到master,这样就可以了
- 【LeetCode每天一题】Valid Parentheses(有效的括弧)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- Centos 7 设置ssh只允许特定用户从指定的IP登录
1.编辑文件 /etc/ssh/sshd_config vi /etc/ssh/sshd_config 2.root用户只允许在如下ip登录AllowUsers root@203.212.4.117A ...
- Photobucket不能用了怎么办?推荐10个在线图片储存服务!
近日,图片共享服务网站Photobucket更新了政策,要求用户缴纳399美元的年费,才能使用第三方网站图片储存服务. 由于Photobucket出台这一政策,亚马逊及其他电商平台上相关的产品图片已被 ...
- C++ 使用openssl库实现 DES 加密——CBC模式 && RSA加密——公加私解——私加公解
之前工作上需要用C++把软件生成的用户序列号用des加密cbc的模式,加密后为二进制,转化为十六进制,然后提供给java写的授权码管理平台. java平台会根据用户序列号,生成一个授权码,授权码是用r ...
- 004-全局应用程序类Global.asax
服务器对象:Request.Response.Server.Session.Application.Cookie //功能1:为服务器对象注册Start.End处理 protected void Ap ...
- yii2 modules模块配置指南
在Yii2 中模块是可以无限级嵌套的,也就是说,模块可以包含另一个包含模块的模块,我们称前者为父模块,后者为子模块, 子模块必须在父模块的yiibaseModule::modules属性中申明,例如: ...
- 09 查找列表中元素,移除每个元素的空格,并查找以a或A开头并且以c结尾的所有元素
li = ["alex"," aric","Alex","Tony","rain"]for i in ...
- asp.net GridView增加删除功能
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string id ...
- Abandoned country(最小生成树+树形DP)
#include<bits/stdc++.h> using namespace std; struct node{ int u, v, w, nex; bool gone; node(){ ...