您的位置:首页技术文章
文章详情页

Python使用sftp实现传文件夹和文件

【字号: 日期:2022-06-21 17:32:46浏览:2作者:猪猪

利用python的sftp实现文件上传,可以是文件,也可以是文件夹。

版本Python2.7.13 应该不用pip安装更多的插件,都是自带的

不多说 上代码

# -*- coding:utf-8 -*-import sysreload(sys)sys.setdefaultencoding(’utf-8’)import paramikoimport os_XFER_FILE = ’FILE’_XFER_DIR = ’DIR’class MainWindow(object): # 构造方法 def __init__(self, arg):# 超类调用super(MainWindow, self).__init__()# 赋值参数[字典]# 参数格式 arg = {’ip’:’填ip’,’user’:’用户名’,’password’:’密码’,’port’:22}self.arg = arg# 赋值参数[FTP]self.sftp = None# 调试日志print self.arg # 启动程序 def startup(self):# 连接FTPif self.sftp != None: print u’您已经成功连接了’tmpstr = u’开始连接...用户名:’+self.arg[’user’]+u’ 密码:’+self.arg[’password’]+’ IP:’+self.arg[’ip’]+u’ 端口:’+str(self.arg[’port’])print tmpstrtry: transport = paramiko.Transport((self.arg[’ip’], self.arg[’port’])) transport.connect(username=self.arg[’user’], password=self.arg[’password’]) self.sftp = paramiko.SFTPClient.from_transport(transport) print (u’连接成功 ’+self.arg[’ip’])except Exception as e: print u’连接失败:’+str(e) # 关闭程序 def shutdown(self):# 关闭FTPif self.sftp: self.sftp.close() print ’### disconnect sftp server: %s!’%self.arg[’ip’] self.sftp = None # 处理上传 def upload(self, source, target, replace):### 操作数据# 来源路径source = source.replace(’’, ’/’)# 目标路径target = target.replace(’’, ’/’)### 验证数据if not os.path.exists(source): print u’来源资源不存在,请检查:’ + source return### 格式数据# 格式化目标路径self.__makePath(target)### 处理数据# 文件媒体数据(文件类型, 文件名称)filetype, filename = self.__filetype(source)# 判断文件类型if filetype == _XFER_DIR: # 1.目录 self.uploadDir(source, target, replace)elif filetype == _XFER_FILE: # 2.文件 self.uploadFile(source, filename, replace) # 传送目录 def uploadDir(self, source, target, replace):### 验证数据# 判断目录存在if not os.path.isdir(source): print u’这个函数是用来传送本地目录的’ return### 处理数据# 遍历目录内容,上传资源for file in os.listdir(source): # 资源路径 filepath = os.path.join(source, file) # 判断资源文件类型 if os.path.isfile(filepath): # 1.文件self.uploadFile(filepath, file, replace) elif os.path.isdir(filepath):# 2.目录try: self.sftp.chdir(file) except: self.sftp.mkdir(file) self.sftp.chdir(file) self.uploadDir(filepath, file, replace)### 重置数据# 返回上一层目录self.sftp.chdir(’..’) # 传送文件 def uploadFile(self, filepath, filename, replace):### 验证数据# 验证文件类型if not os.path.isfile(filepath): print u’这个函数是用来传送单个文件的’ return# 验证文件存在if not os.path.exists(filepath): print u’err:本地文件不存在,检查一下’+filepath return# 验证FTP已连接if self.sftp == None: print u’sftp 还未链接’ return### 处理数据# 判断文件存在是否覆盖if not replace: if filename in self.sftp.listdir():print u’[*] 这个文件已经存在了,选择跳过:’ + filepath + ’ -> ’ + self.sftp.getcwd() + ’/’ + filenamereturn# 上传文件try: self.sftp.put(filepath, filename) print u’[+] 上传成功:’ + filepath + ’ -> ’ + self.sftp.getcwd() + ’/’ + filenameexcept Exception as e: print u’[+] 上传失败:’ + filepath + ’ because ’ + str(e) # 获得文件媒体数据({文件/目录, 文件名称}) def __filetype(self, source):# 判断文件类型if os.path.isfile(source): # 1.文件 index = source.rfind(’/’) return _XFER_FILE, source[index+1:]elif os.path.isdir(source): # 2.目录 return _XFER_DIR, ’’ # 创建目标路径 # 说明: 目标路径不存在则依次创建路径目录 def __makePath(self, target):# 切换根目录self.sftp.chdir(’/’)# 分割目标目录为目录单元集合data = target.split(’/’)# 进入目标目录, 目录不存在则创建for item in data: try:self.sftp.chdir(item) print u’要上传的目录已经存在,选择性进入合并:’ + item except:self.sftp.mkdir(item)self.sftp.chdir(item) print u’要上传的目录不存在,创建目录:’ + itemif __name__ == ’__main__’: # ''' # 先熟悉一下sftp有哪些用法 sftp.listdir(可以传参可以为空) 返回当前目录下清单列表 # mkdir 创建目录对应rmdir sftp.put(本地路径,远程要存的文件名) chdir进入子目录 # ''' arg = {’ip’:’填ip’,’user’:’填用户名’,’password’:’填密码’,’port’:22} me = MainWindow(arg) me.startup() # 要上传的本地文件夹路径 source = r’E:xamppbackupmysqlcto’ # 上传到哪里 [远程目录] target = r’/home/www/cto/wp-superdo/backup/db’ replace = False me.upload(source, target, replace) me.shutdown()def main(source, target, replace=False): arg = {’ip’:填ip,’user’:填用户名,’password’:填密码,’port’:22} me = MainWindow(arg) me.startup() me.upload(source, target, replace) me.shutdown()

因为Python2.7对中文的支持不是很好所以如果出现中文错误修改一下 Python27Libsite-packagesparamikopy3compat.py

Python使用sftp实现传文件夹和文件

还有

Python使用sftp实现传文件夹和文件

最后上一下执行结果

Python使用sftp实现传文件夹和文件

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持好吧啦网。

标签: Python 编程
相关文章: