python:检测ftplib retrlines上的连接丢失

2024-05-15 09:21:41 发布

您现在位置:Python中文网/ 问答频道 /正文

通过ftplib从不同的目录下载大量文件。使用retrline下载。下面的代码片段用于循环中100个文件。fn是指远程文件名。在

我正在尝试找到一个干净的方法来获取错误号,以便在错误号大于等于400时恢复连接。我想我应该从ftplib.all\u错误但是错误是不一致的。。。所以让黑客来解析消息。在

我现在对例外的形式感到困惑。。。e应该是元组。。。但我发现我可以参考e.errno/e.strerror。由于FTP RETR错误(8192行)是间歇性的。。。不知道哪一个是最好的方法,所以我决定张贴这个问题。任何提示都会有帮助。在

   try:
        results = ftpID.retrlines("RETR " + fn, handler)
        return True
   except ftplib.all_errors as e:
        logging(str(cmd) + " (gettxt.ftp)"
                     +"FTP ERROR({0}): {1} on {2]".format(e.errno, e.strerror, fn))
        # Check for connection lost, line > 8192  -- connection is lost
        # Doc on ftp exception ftplib.all_errors is unclear about errno. messy
        # some str(e) does not give errno but others do?
        # OLD WAY: if str(e).find('8192') != -1 :
        if e.strerror.find('8192') != -1 :
            logging(" : Fatal FTP ERROR - connection lost")
            close()
            ftpID = None
        # try: self.ftpID.voidcmd("NOOP") ; exception: self.ftpID = None; return None
        return False
    except Exception as e:
        logging(str(cmd) + " (gettxt) " + str(e))
        return False

另外,我知道文件: http://docs.python.org/2/library/ftplib.html#ftplib.error_replyhttp://docs.python.org/2/library/ftplib.html#ftplib.all_errors 看了十个例外的帖子。。。对于异常的高级处理仍然不清楚。在


Tags: nonereturnlogging错误ftpallconnectionfn