Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set timeout for FTP connection in Python with ftplib

I am triying to set up the timeout of a FTP connection usign:

class ftplib.FTP([host[, user[, passwd[, acct[, timeout]]]]])

Return a new instance of the FTP class. When host is given, the method call connect(host) is made. When user is given, additionally the method call login(user, passwd, acct) is made (where passwd and acct default to the empty string when not given). The optional timeout 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).

The problem is that my code to create the connection is as follows:

from ftplib import FTP
ftp = FTP('172.16.52.87')
ftp.login('username', 'password')

I found some problems if I use:

ftp = FTP('172.16.52.87', 'username', 'password')

Then my question is, how can I set up th timeout ?

I have tried let some parameters empty but it does not work:

ftp = FTP('172.16.52.87', '', '', '', '100')

And login function has only 3 parameters login(user, passwd, acct)

Some idea?

Regards

like image 885
David Comino Avatar asked Mar 31 '15 17:03

David Comino


1 Answers

Try:

ftp = FTP('172.16.52.87', timeout=100)
ftp.login('user', 'pass)

or even

ftp = FTP('172.16.52.87', 'user', 'pass', timeout=100)

References:

  • https://docs.python.org/2/tutorial/controlflow.html#keyword-arguments
  • http://www.diveintopython.net/power_of_introspection/optional_arguments.html
like image 182
Robᵩ Avatar answered Oct 04 '22 07:10

Robᵩ