python ftp timeout_Python FTP套接字超时处理

我在使用python35ftplib处理套接字超时时遇到问题。当发生套接字超时错误时,由于某种原因,我无法捕获异常,脚本无论如何都会引发错误并退出。以下是相关代码块:try:

ftp = FTP(self.config.base_url, timeout=400)

ftp.login()

ftp.cwd(self.config.root_path)

ftp.retrbinary("RETR {0}".format(os.path.join(self.root_path, file_path)), fp.write, 1024)

ftp.quit()

except socket.timeout:

self.download_file(file_path)

由于某些原因,此脚本仍然会出现套接字超时异常,这是怎么可能的呢?一概而论也行不通。以下是错误的堆栈跟踪:

^{pr2}$

如你所见,这是套接字超时抛出错误,那么如何不被捕获?经过数小时的互联网研究,我无法找到任何关于如何解决这个问题的有用信息,对此问题的任何见解都将不胜感激。在

以下是插座.py公司名称:def readinto(self, b):

"""Read up to len(b) bytes into the writable buffer *b* and return

the number of bytes read. If the socket is non-blocking and no bytes

are available, None is returned.

If *b* is non-empty, a 0 return value indicates that the connection

was shutdown at the other end.

"""

self._checkClosed()

self._checkReadable()

if self._timeout_occurred:

raise OSError("cannot read from timed out object")

while True:

try:

return self._sock.recv_into(b)

except timeout:

self._timeout_occurred = True

raise

except error as e:

if e.args[0] in _blocking_errnos:

return None

raise

更新:

问题似乎是,如果传递给FTP构造函数的超时大于套接字超时的默认值,ftplib的行为会出乎意料。在进行此编辑时,有一个开放的python问题来解决此行为:http://bugs.python.org/issue30956

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值