1

リモート FTP サーバーに接続してファイルをダウンロードする Python スクリプトがあります。私が接続しているサーバーはあまり信頼性が高くないため、転送が停止したり、転送速度が非常に遅くなったりすることがよくあります。ただし、エラーは発生しないため、スクリプトも停止します。

ftplib関数でモジュールを使用しますretrbinary。ダウンロードが中止されるまでのタイムアウト値を設定してから、転送を自動的に再試行/再開できるようにしたいと考えています (再開は便利ですが、ファイルが 300M 以下であるため、厳密には必要ではありません)。

4

2 に答える 2

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()
于 2013-06-21T11:23:46.297 に答える