【问题标题】:Python SFTP issue With Sockets Closing - An existing connection was forcibly closed by the remote host (10054) - How to continue? Breaks process,套接字关闭的 Python SFTP 问题 - 远程主机强制关闭现有连接 (10054) - 如何继续?中断过程,
【发布时间】:2020-12-17 15:32:31
【问题描述】:

所以我有一个过程,我将一系列目录和文件发送到服务。服务处理这些文件,然后将目录和输出文件返回到不同的目录

例如,将有一个带有运行名称的父目录,然后是我们正在处理的每个日期的子目录..即:

输入

RunName

    2020-06-01
    2020-06-02
    2020-06-03

该服务在输出下为运行名称创建一个等效目录,然后在它们处理数据时创建相同的目录,因此当它处理第一个文件时,它看起来像这样:

输出

RunName

    2020-06-01
    
    

当服务处理完所有文件后,它会将一个“完成”文件放入包含所有日期的目录中。一旦收到 donefile,我的程序就可以进入下一个处理阶段。

我要做的是确定处理完成的百分比,然后确定何时完成。

下面的代码完美无缺,除了一件事

我经常得到

ERROR:paramiko.transport:Socket 异常:现有连接被远程主机强行关闭 (10054) 指向第二行(以“While”开头)

我需要进行哪些更改才能强制重新连接,然后在发生此错误时重试,并让 while 循环继续沿它的方式继续。

任何帮助将不胜感激..这让我很生气,因为它中断了一个更大的过程并导致积压。

srv = pysftp.Connection(host=servername, username=user,password=pwd,cnopts=cnopts)

while srv.isfile(donefile)==False:
    try:
        srv = pysftp.Connection(host=servername, username=user,password=pwd,cnopts=cnopts)
        dirnames=[]
        for i in srv.listdir(outputdir):
            if i[:6]=="date=2":
                dirnames.append(i)


        srv.close()
        if max(dirnames)!=lastname:
            print("Last Directory is: " ,max(dirnames), "out of", max(dirnamesin), " ",round((len(dirnames)/len(dirnamesin))*100), "pct done", datetime.datetime.now(), "run: ",run_name)
        lastname=max(dirnames)
        time.sleep(30)
        srv = pysftp.Connection(host=servername, username=user,password=pwd,cnopts=cnopts)
    except:
        print("connection error - will try again in 30 seconds")
        time.sleep(30)
        srv = pysftp.Connection(host=servername, username=user,password=pwd,cnopts=cnopts)
print ('done')

【问题讨论】:

    标签: python paramiko pysftp


    【解决方案1】:

    我会尝试重构它以确保正确关闭连接,例如

    while True:
        try:
            with pysftp.Connection(host=servername, username=user,password=pwd,cnopts=cnopts) as srv:
                if srv.isfile(donefile):
                    break
                dirnames=[]
                for i in srv.listdir(outputdir):
                    if i[:6]=="date=2":
                        dirnames.append(i)
            if max(dirnames)!=lastname:
                print("Last Directory is: " ,max(dirnames), "out of", max(dirnamesin), " ",round((len(dirnames)/len(dirnamesin))*100), "pct done", datetime.datetime.now(), "run: ",run_name)
            lastname=max(dirnames)
        except Exception:
            print("connection error - will try again in 30 seconds")
        time.sleep(30)
    print('done')
    

    【讨论】:

      猜你喜欢
      • 2012-02-07
      • 2011-06-06
      • 1970-01-01
      • 2018-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-20
      • 1970-01-01
      相关资源
      最近更新 更多