Arl*_*rke 6 python ftp python-3.x socket-timeout-exception
我在使用python35 ftplib处理套接字超时时遇到问题。当发生套接字超时错误时,由于某种原因,我无法捕获该异常,并且脚本无论如何都会引发错误并退出。这是相关的代码块:
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)
Run Code Online (Sandbox Code Playgroud)
由于某种原因,该脚本仍会因套接字超时异常而出错,这怎么可能?通用包也不行。这是错误的堆栈跟踪:
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)
Run Code Online (Sandbox Code Playgroud)
如您所见,这是抛出的socket.timeout错误,那么怎么不捕获呢?经过数小时的互联网研究,我找不到任何有关如何解决此问题的有用信息,对此问题的任何见解将不胜感激。
作为参考,这是socket.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
Run Code Online (Sandbox Code Playgroud)
更新:看来问题在于,如果传递给FTP构造函数的超时大于套接字超时的默认值,则ftplib会表现异常。在进行此编辑时,有一个开放的python问题来解决此问题:http : //bugs.python.org/issue30956
| 归档时间: |
|
| 查看次数: |
1725 次 |
| 最近记录: |