I am trying to use ftplib.FTP()
with timeout option as some timeout value for a particular hostname. But i am experiencing weird behaviour. To test it i have written a very simple piece of code.
import ftplib
from ftplib import FTP
ftp = ftplib.FTP("google.com",timeout=2)
The API document says to enter timeout value in seconds, but it seems that it takes longer than that, for me it almost takes more than 8 secs. Can anybody please explain the behaviour.I am using python2.7
From
ftplib.FTP
docs:i.e., the timeout may limit individual socket operations but it says nothing about the duration of the
FTP()
call itself.As @user590028 pointed out: FTP calls (indirectly)
socket.create_connection()
that may invoke several blocking operations in sequence and it may succeed if each operation takes less than timeout seconds even if all operations combined take longer.If you want to enforce the total timeout, see Timeout on a Python function call.
ftplib.FTP
invokessocket.create_connection()
. According to the docs https://docs.python.org/2/library/socket.html#socket.create_connectionA quick check of
google.com
will show about a dozen (or more) depending on your region of the country. Your 2 second timeout is applied to each of the hosts.If you want to limit total time to 2 seconds, do the lookup first and pass the numeric address to your
ftplib.FTP
call: