假设我想使用ftplib在Python中使用FTP。我首先要说的是:
from ftplib import ftp
ftp = FTP('10.10.10.151')但是,如果FTP服务器没有联机,它将无限期地挂在那里。据我所知,唯一能把它踢出来的就是键盘中断。我试过这个:
ftp.connect('10.10.10.151','21', 5)而这5分钟是5秒钟暂停。但这里的问题是,我不知道在不首先分配ftp的情况下使用该行的任何方法。但是如果服务器离线,那么"ftp =“行将挂起。那么ftp.connect()的超时函数有什么用呢?!?
有人知道解决办法吗?有没有办法超时我还没有找到的"ftp = FTP(xxx)“命令?谢谢。
我在Linux上使用Python2.7。
发布于 2014-10-02 19:19:26
对connect()的调用是多余的,因为FTP()方法文档声明:
When host is given, the method call connect(host) is made.此外,由于Python2.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).https://stackoverflow.com/questions/24703742
复制相似问题