Reputation: 360
I have a script (below) that pushes an html file to a server. It WORKS 95% of the time. However, about 5% of the time, the server fails and the attempt to connect hangs. It stalls for 15 minutes (900 seconds) and then fails.
ftp = FTP('[FTP ADDRESS HERE]')
ftp.login('[USERNAME]','[PASSWORD]')
converted_HTML_string = io.BytesIO(HTML_string)
ftp.storbinary('STOR public_html/home.html', converted_HTML_string)
Again, this works 95% of the time. In the 5% when it doesn't, if I manually interrupt it when it hangs and simply re-run the request again -- then it works. It seems to be caused by temporary server glitches. I have no control over the server quality.
The real problem is that the hang is too long. What I want to be able to set a time limit (like 30 seconds) on how long it hangs, and then have code that says "re-run this ftp command until it succeeds."
Is that possible? Is there another way I should handle this?
Thank you!
Upvotes: 3
Views: 8445
Reputation: 33
This error had me thinking fo a while.
I made an exception in an infinite loop while True
so when it happens it still True
then I call my function ftp_connect_to_site() that is inside a class named RemoteTar
This function is going to reconnect you if this exception happens Not the best Idea.
am a beginner in python that all I could think of
except ftplib.error_temp as e:
print('Time run out reconnect: ',e)
RemoteTar.ftp_connect_to_site()
Upvotes: 2
Reputation: 77837
The channel timeout is set on the ftp server. Some interfaces (such as FileZilla) allow you to configure your channel timeout. Fortunately, Python's FTP package is one of these. See the documentation for further details. In the meantime, just try the timeout parameter:
ftp = FTP('[FTP ADDRESS HERE]', timeout=30)
Maxim Lott (OP) was able to catch the timeout error with the following, which re-tries the connection until it works:
import socket
except socket.timeout, e:
Upvotes: 6