Sunday, 15 June 2014

python - how to abort and retry ftp download after specified time? -


I have a Python script that connects to a remote FTP server and downloads a file. As I connect to the server not is very reliable, it often happens that transfer stalls and transfer rates are extremely low, however, no error has been raised, so that my script is also stalled Ho.

I am using the ftplib module with the retrbinary function. I would like to be able to set a timeout value after which the download will stop, and then restart / restart the transfer automatically (it will be good to resume, but it is not strictly necessary because the files are only ~ 300m ).

I've managed to use the threading module :

  conn = FTP (hostname, timeout = 60.) Conn.set_pasv (True) conn.login () while correct: try localfile = open (local_filname, "wb"): dlthread = Threading.Thread (target = conn .retrbinary, args = ("RETR {0}" format (remote_filename), local file.write)) dlthread.start () dlthread.join (timeout = 60.) if not dlthread.is_alive ( ): Brake DelBoth Print ("Download (Timeout)" Waiting for 10 "... format (timeout = 60)) time.sleep (10) Keyboard (except" Redirect Thread "), Keyboard Interrupts: Extend addition: Local file pass .close ()    

No comments:

Post a Comment