【发布时间】:2013-06-18 07:36:48
【问题描述】:
我有一个 Python 脚本,它连接到远程 FTP 服务器并下载文件。由于我连接的服务器不是非常可靠,因此经常会发生传输停止和传输速率变得非常低的情况。但是,没有引发错误,因此我的脚本也停止了。
我使用ftplib 模块和retrbinary 函数。我希望能够设置一个超时值,之后下载中止,然后自动重试/重新启动传输(恢复会很好,但这不是绝对必要的,因为文件只有 ~300M)。
【问题讨论】:
我有一个 Python 脚本,它连接到远程 FTP 服务器并下载文件。由于我连接的服务器不是非常可靠,因此经常会发生传输停止和传输速率变得非常低的情况。但是,没有引发错误,因此我的脚本也停止了。
我使用ftplib 模块和retrbinary 函数。我希望能够设置一个超时值,之后下载中止,然后自动重试/重新启动传输(恢复会很好,但这不是绝对必要的,因为文件只有 ~300M)。
【问题讨论】:
我使用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()
【讨论】:
【讨论】:
retrbinary没有任何影响(只对connect)