Python: Control timeout length

Posted by skylarking on Stack Overflow See other posts from Stack Overflow or by skylarking
Published on 2010-03-01T12:34:00Z Indexed on 2010/04/28 18:07 UTC
Read the original article Hit count: 197

Filed under:

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.

© Stack Overflow or respective owner

Related posts about python