I have code similar to the following running in a script:
try:
s = ftplib.FTP('xxx.xxx.xxx.xxx','username','password')
except:
print ('Could not contact FTP serer')
sys.exit()
IF the FTP site is inaccessible, the script almost seems to ‘hang’ … It is taking about 75 seconds on average before sys.exit() appears to be called… I know the 75 seconds is probably very subjective, and dependent on the system this runs on…but is there a way to have python just try this once, and if unsucessful, to exit immediately? The platform I am using for this is Mac OS X 10.5/python 2.5.1.
Starting with 2.6, the
FTP constructorhas an optionaltimeoutparameter:Starting with version 2.3 and up, the global default timeout can be utilized: