【问题标题】:how to abort and retry ftp download after specified time?如何在指定时间后中止并重试 ftp 下载?
【发布时间】:2013-06-18 07:36:48
【问题描述】:

我有一个 Python 脚本,它连接到远程 FTP 服务器并下载文件。由于我连接的服务器不是非常可靠,因此经常会发生传输停止和传输速率变得非常低的情况。但是,没有引发错误,因此我的脚本也停止了。

我使用ftplib 模块和retrbinary 函数。我希望能够设置一个超时值,之后下载中止,然后自动重试/重新启动传输(恢复会很好,但这不是绝对必要的,因为文件只有 ~300M)。

【问题讨论】:

    标签: python ftp timeout ftplib


    【解决方案1】:

    我使用threading 模块管理了我需要做的事情:

     conn = FTP(hostname, timeout=60.)
     conn.set_pasv(True)
     conn.login()
     while True:
         localfile = open(local_filename, "wb")
         try:
             dlthread = threading.Thread(target=conn.retrbinary,
                     args=("RETR {0}".format(remote_filename), localfile.write))
             dlthread.start()
             dlthread.join(timeout=60.)
             if not dlthread.is_alive():
                 break
             del dlthread
             print("download didn't complete within {timeout}s. "
                     "waiting for 10s ...".format(timeout=60))
             time.sleep(10)
             print("restarting thread")
         except KeyboardInterrupt:
             raise
         except:
             pass
         localfile.close()
    

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 来自您的链接:“可选的 timeout 参数指定连接尝试等阻塞操作的超时时间(以秒为单位)”。我刚试了,超时参数好像对retrbinary没有任何影响(只对connect
      猜你喜欢
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 2015-03-27
      • 2016-05-03
      • 2016-11-10
      • 1970-01-01
      • 2011-10-19
      相关资源
      最近更新 更多