Python和SFTP

Python的SFTP功能实现

最近公司需要使用SFTP进行文件的传输,试着用Python实现了一下,主要用到的模块是paramiko。该模块详细的文档可以参考下面的网址内容:http://www.paramiko.org/

代码如下,Python新手,仅供参考喽:

主要实现的功能:

  • 从指定的SFTP路径下下载文件到本地
  • 在本地下载路径下生成日志文件,记录下载内容
  • 文件下载失败记录为下载文件,跳过该文件继续下载

需要注意的关键点:

  • get方法只能必须具体到文件,因此需要对目录下的所有文件进行遍历,然后下载
  • continue的使用让下载某个文件失败后可以继续下载
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 14 11:47:55 2016

@author: Neil

Test for Sftp
"""

import paramiko
import sys
import time
import os


class PySFTP(object):

    def __init__(self):

        print('Create a SFTP Deload Project!')

    def connectSFTP(self,remotename,remoteport,loginname,loginpassword):

        try:
            sftp = paramiko.Transport(remotename,remoteport)
            print('connect success!')
        except Exception as e:
            print('connect failed,reasons are as follows:',e)
            return (0,'connect failed!')
        else:
            try:
                sftp.connect(username = loginname,password = loginpassword)
                print('login success!')
            except Exception as e:
                print('connect failed,reasons are as follows:',e)
                return (0,'login failed!')
            else:
                return (1,sftp)
    def download(self,remotename,remoteport,loginname,loginpassword,remoteaddress,localaddress):

        sftp = self.connectSFTP(remotename,remoteport,loginname,loginpassword)

        if sftp[0] != 1:
            print(sftp[1])
            sys.exit()

        sftp = paramiko.SFTPClient.from_transport(sftp[1])

        filelist = sftp.listdir(remoteaddress)

        filelist = filelist[:2]#测试时仅下载了2个文件

        print('begin downloading!')
        for i in filelist:
            try:              
                start = time.clock()
                sftp.get(remoteaddress+'/'+i,localaddress+'\\'+i)
                end = time.clock()
                print('success download %s,use time %fs' % (i,(end - start)))               
            except Exception as e:
                print('failed download %s,reason are as follows:' % i,e)
                with open(r'C:\Users\Neil\Desktop\Test\time.log','a') as f:
                    f.write('failed download %s,reason are as follows:' % i,e)
                continue #下载出错继续进行下一步
            else:
                with open(r'C:\Users\Neil\Desktop\Test\time.log','a') as f:
                    f.write('success download %s\n' % i)

def main():
    sftp = PySFTP()
    sftp.download(remotename = 'SFTP的host地址',
                              remoteport=21,     #SFTP的默认端口号是21
                              loginname = '用户名',
                              loginpassword = '密码',
                              remoteaddress='下载文件所在的路径',
                              localaddress = r'需要下载到的地址路径') 


if __name__ == '__main__':
    main()

其实关于sftp的下载,还可以实现未正确下载的文件再次下载,直到所有文件全部下载完毕,有兴趣的小伙伴可以实现一下^-^

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值