我在使用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