Python sftp上传文件 速度过慢

本文介绍了通过优化Python的paramiko库实现SFTP连接,提高文件上传和目录创建速度的方法。针对服务器在国外,网络延迟大的情况,作者分析了文件目录增多导致的性能下降问题,并提出通过避免遍历目录检查是否存在,直接尝试创建目录来提升效率。经过优化,文件上传和目录创建的时间显著减少,从原本的5分钟缩短到1分钟内完成。
摘要由CSDN通过智能技术生成

原因:服务器在国外
我方:国内
网络问题较大,自己用软件测试(winscp)同样很慢刚开始文件很少,读取目录速度没有那么慢,时间长了,文件变多

第一版代码

class SftpInfo(object):
    def __init__(self, username, password, timeout=3000):
        self.username = username
        self.password = password
        self.timeout = timeout
        # transport和chanel
        self.t = ''
        self.chan = ''
        # 链接失败的重试次数
        self.try_times = 10

    # 调用该方法连接远程主机
    def connect(self):
        while True:
            # 连接过程中可能会抛出异常,比如网络不通、链接超时
            try:
                self.t = paramiko.Transport(sock=('xxxxxxxxxxx', 22))
                print(self.username)
                print(self.password)

                self.t.connect(username=self.username, password=self.password)
                # 如果没有抛出异常说明连接成功,直接返回
                print(u'连接%s成功')
                return
            # 这里不对可能的异常如socket.error, socket.timeout细化,直接一网打尽
            except Exception as e1:
                if self.try_times != 0:
                    print(u'连接%s失败,进行重试')
                    self.try_times -= 1
                else:
                    print(u'重试10次失败,结束程序')
                    exit(1)

    def sftp_upload_file(self, remote_dir, file, filename):
        # 去掉路径字符穿最后的字符'/',如果有的话
        remote_filename = remote_dir + '/' + filename
        sftp = paramiko.SFTPClient.from_transport(self.t)
        is_existence = True if filename in sftp.listdir(f'{remote_dir}/') else False
        if not is_existence:
            sftp.putfo(fl=file, remotepath=remote_filename)
        return True

    def sftp_download_file(self, remote_file_name, file_name):
        sftp = paramiko.SFTPClient.from_transport(self.t)
        # is_existence = True if file_name in sftp.listdir(f'{remote_file_name}/') else False
        # if is_existence:
        sftp.get(remote_file_name, file_name)  # 远程路径 本地路径


    def sftp_mkdir_dir(self, dir_name, purchase_currency_code):
        sftp = paramiko.SFTPClient.from_transport(self.t)
        is_existence = True if dir_name in sftp.listdir('T/') else False  # 哦按段
        if not is_existence:
              sftp.mkdir(path=f'/{dir_name}')
         return f'T/{dir_name}'
    # 断开连接
    def close(self):

        self.t.close()
        print('stfp--------------------->断开连接')

   

我这里使用的是 sftp.putfo 我这边是数据流可以更换自己想要的哈
时间测试在5分钟内可以上传完

第二版
修改这个方法

def sftp_mkdir_dir(self, dir_name, purchase_currency_code):
        sftp = paramiko.SFTPClient.from_transport(self.t)
        # is_existence = True if dir_name in sftp.listdir('T/') else False  # 哦按段
        # if not is_existence:
         try:
         	sftp.mkdir(path=f'/{dir_name}')
         except Exception as  e:
         		# 如果存在了会报错已经存在 这个错误pass处理掉
                print(e)
         return f'T/{dir_name}'

时间测试在1分钟内可以上传完
原因:文件目录越多,打开这个目录下的文件越慢,底层需要循环遍历,判断这个文件名是否已经存在

在别人服务器执行的是命令,可以直接执行创建命令,不遍历目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值