You can specify a timeout
parameter in the FTP
constructor (as long as you're running 2.6+).
class ftplib.FTP([host[, user[, passwd[, acct[, timeout]]]]])
Return a new instance of the
FTP
class. Whenhost
is given, the method callconnect(host)
is made. Whenuser
is given, additionally the method calllogin(user, passwd, acct)
is made (wherepasswd
andacct
default to the empty string when not given). The optionaltimeout
parameter specifies a timeout in seconds for blocking operations like the connection attempt (if is not specified, the global default timeout setting will be used).
I believe subsequent blocking operations that take longer than your timeout
will raise socket.timeout
exception. You can catch those and retry as needed.