【问题标题】:Python FTP hangPython FTP 挂起
【发布时间】:2014-07-11 17:57:14
【问题描述】:

假设我想使用 ftplib 在 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。

【问题讨论】:

    标签: timeout freeze connection-timeout


    【解决方案1】:

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

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

    另外,从 Python 2.6 开始,FTP() 确实有一个 timeout 参数:

    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).
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 2013-03-11
      • 1970-01-01
      • 2012-01-19
      • 2016-03-28
      • 1970-01-01
      相关资源
      最近更新 更多