2014-07-11 134 views
0

说我想用ftplib在Python中使用FTP。我从这开始:Python FTP挂起

from ftplib import ftp 
ftp = FTP('10.10.10.151') 

但是,如果FTP服务器不在线,它将无限期地挂在那里。据我所知,唯一能够将它踢出去的是键盘中断。我试过这个:

ftp.connect('10.10.10.151','21', 5) 

随着五是五秒超时。但这里的问题是,我不知道如何在没有首先分配ftp的情况下使用该行。但是,如果服务器处于脱机状态,则“ftp =”行将挂起。那么ftp.connect()的超时函数有什么用?!?

有没有人知道解决方法或任何东西?有没有办法超时我没有找到的“ftp = FTP(xxx)”命令?谢谢。

我在Linux Mint上使用Python 2.7。

回答

0

您对connect()调用是多余的,因为FTP()方法documentation状态:

When host is given, the method call connect(host) is made. 

而且,因为Python 2.6,FTP()确实有超时参数:

class ftplib.FTP([host[, user[, passwd[, acct[, timeout]]]]]) 
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).