2014-07-11 133 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).