PYTHON利用SFTP进行单或多文件下载

#!/usr/bin/python
# coding=utf-8
import paramiko
import os,time
import configparser

curpath=os.path.realpath("ip.ini")
conf=configparser.ConfigParser()
#conf=configparser.configparser()
conf.read(curpath)

ip_list=conf.get("ip_list","ftpInfo")
user=conf.get("ip_list","user")
psw=conf.get("ip_list","psw")
ftppath_list=conf.get("ip_list","ftppath")
local_path=conf.get("ip_list","dstfolder")
f=conf.get("ip_list","rules")
nun_f=f.split('|')
#print(find_files)


def log(s):
  T = time.strftime("%Y%m%d-%H:%M:%S", time.localtime())
  print(T + '\t' + s)
  with open("log.txt", "a") as fp:
    fp.write(T + '\t' + s + '\n')



def sftp_download(host,username,password,local,remote):
    sf = paramiko.Transport((host))
    sf.connect(username =username,password=password)
    log("host,username:"+host+","+username)
    sftp = paramiko.SFTPClient.from_transport(sf)
    print("LOGGIN SUCCESS!")
    log("loggin sucess!")
    log("find file:"+f)
    try:

        if os.path.isdir(local):#判断本地参数是目录还是文件

            for n_f in sftp.listdir(remote):
                n_f1=n_f
                #log(n_f1)
                if n_f.split("=")[2]==f:
                    for sub_f in sftp.listdir(remote+n_f1+"/"):
                        print(sub_f)
                        log(sub_f)
                        sftp.get(os.path.join(remote+n_f1+"/"+sub_f),os.path.join(local+sub_f))
        else:
            sftp.get(remote,local)

        #except Exception as ex:
    except Exception as ex:
        print('download exception:', ex)

        sf.close()
if __name__ == '__main__':
    host=ip_list#主机
    #port = 22 #端口
    username =user #用户名
    password = psw #密码
    local = local_path#本地文件或目录,与远程一致,当前为windows目录格式,window目录中间需要使用双斜线
    remote =ftppath_list#远程文件或目录,与本地一致,当前为linux目录格式
    #sftp_upload(host,port,username,password,local,remote)#上传
    sftp_download(host,username,password,local,remote)#下载


*************************************************************************
多文件下载
!/usr/bin/python
# coding=utf-8
import paramiko
import os,time
import configparser

curpath=os.path.realpath("ip.ini")
conf=configparser.ConfigParser()
#conf=configparser.configparser()
conf.read(curpath)

ip_list=conf.get("ip_list","ftpInfo")
user=conf.get("ip_list","user")
psw=conf.get("ip_list","psw")
ftppath_list=conf.get("ip_list","ftppath")
local_path=conf.get("ip_list","dstfolder")
f=conf.get("ip_list","rules")
num_f=f.split('|')
#print(find_files)


def log(s):
  T = time.strftime("%Y%m%d-%H:%M:%S", time.localtime())
  print(T + '\t' + s)
  with open("log.txt", "a") as fp:
    fp.write(T + '\t' + s + '\n')



def sftp_download(host,username,password,local,remote):
    sf = paramiko.Transport((host))
    sf.connect(username =username,password=password)
    log("host,username:"+host+","+username)
    sftp = paramiko.SFTPClient.from_transport(sf)
    print("LOGGIN SUCCESS!")
    log("loggin sucess!")
    log("find file:"+f)
    try:

        if os.path.isdir(local):#判断本地参数是目录还是文件
            x=1
            while x <= len(num_f):
                muli_f = num_f[x - 1]
                print("downing..." + muli_f)
                log(muli_f)
                for n_f in sftp.listdir(remote):
                    n_f1=n_f

                    #log(n_f1)
                    if n_f.split("=")[2]==muli_f:

                        for sub_f in sftp.listdir(remote+n_f1+"/"):
                            print(sub_f)
                            log(sub_f)
                            sftp.get(os.path.join(remote+n_f1+"/"+sub_f),os.path.join(local+sub_f))
                x = x + 1
        else:
            sftp.get(remote,local)

        #except Exception as ex:
    except Exception as ex:
        print('download exception:', ex)

        sf.close()
if __name__ == '__main__':
    host=ip_list#主机
    #port = 22 #端口
    username =user #用户名
    password = psw #密码
    local = local_path#本地文件或目录,与远程一致,当前为windows目录格式,window目录中间需要使用双斜线
    remote =ftppath_list#远程文件或目录,与本地一致,当前为linux目录格式
    #sftp_upload(host,port,username,password,local,remote)#上传
    sftp_download(host,username,password,local,remote)#下载

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_44390640

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值