小编Arl*_*rke的帖子

Python FTP套接字超时处理

我在使用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 …
Run Code Online (Sandbox Code Playgroud)

python ftp python-3.x socket-timeout-exception

6
推荐指数
1
解决办法
1725
查看次数