用Python捕获ConnectionResetError

用Python捕获ConnectionResetError,python,exception-handling,Python,Exception Handling,我正在构建一个Python脚本,它在我的数据库中搜索所有URL,然后根据URL查找断开的链接。此脚本在打开链接时遇到错误时,需要使用异常处理来记录,但是它开始遇到错误,我一直无法为以下内容编写except语句: Traceback (most recent call last): File "exceptionerror.py", line 97, in <module> raw_response = response.read().decode('utf8', erro

我正在构建一个Python脚本,它在我的数据库中搜索所有URL,然后根据URL查找断开的链接。此脚本在打开链接时遇到错误时,需要使用异常处理来记录,但是它开始遇到错误,我一直无法为以下内容编写except语句:

Traceback (most recent call last):
  File "exceptionerror.py", line 97, in <module>
    raw_response = response.read().decode('utf8', errors='ignore')
  File "/usr/lib/python3.4/http/client.py", line 512, in read
    s = self._safe_read(self.length)
  File "/usr/lib/python3.4/http/client.py", line 662, in _safe_read
    chunk = self.fp.read(min(amt, MAXAMOUNT))
  File "/usr/lib/python3.4/socket.py", line 371, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [Errno 104] Connection reset by peer
以及:

甚至还有一个完整的通用异常,试图捕获所有错误,这样就不会杀死整个脚本:

except:
    print("This link was not caught by defined exceptions: " + articlelinks[j])
    continue
我完全不知道如何让我的脚本捕捉到这个错误,以便它可以继续检查断开的链接,而不是硬失败。它是断断续续的,所以我不相信链接断开了,我觉得即使我已经识别了URL,简单地捕获它并在手之前跳过它也是作弊,因为我的目标是正确处理异常。有人能告诉我如何处理这个异常吗

以下是我的完整循环,仅供参考:

for j in range(0, len(articlelinks)):
    try:
        req=urllib.request.Request(articlelinks[j], None, {'User-agent' : 'Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0'})
        response = urllib.request.urlopen(req)
    except urllib.request.HTTPError as inst:
        brokenlinksflag = 1
        brokenlinks = articlelinks[j] + ' ' + format(inst) + ', ' + brokenlinks
        continue
    except TimeoutError:
        brokenlinksflag = 1
        brokenlinks = articlelinks[j] + ' Timeout Error, ' + brokenlinks
        continue
    except urllib.error.URLError as inst:
        brokenlinksflag = 1
        brokenlinks = articlelinks[j] + ' ' + format(inst) + ', ' + brokenlinks
        continue
    except SocketError as inst:
        brokenlinksflag = 1
        brokenlinks = articlelinks[j] + ' ' + sys.exc_info()[0] + ', ' + brokenlinks
        continue
    except:
        print("This article killed everything: " + articlelinks[j])
        exit()

解决了!问题是我正在对连接进行故障排除以处理ConnectionResetError,但是,对完整错误的更仔细检查表明,错误是通过尝试处理响应而不是打开url引发的:

  File "exceptionerror.py", line 97, in <module>
    raw_response = response.read().decode('utf8', errors='ignore')

你能给我们一个失败的url示例吗?是间歇性失败的URL。
for j in range(0, len(articlelinks)):
    try:
        req=urllib.request.Request(articlelinks[j], None, {'User-agent' : 'Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0'})
        response = urllib.request.urlopen(req)
    except urllib.request.HTTPError as inst:
        brokenlinksflag = 1
        brokenlinks = articlelinks[j] + ' ' + format(inst) + ', ' + brokenlinks
        continue
    except TimeoutError:
        brokenlinksflag = 1
        brokenlinks = articlelinks[j] + ' Timeout Error, ' + brokenlinks
        continue
    except urllib.error.URLError as inst:
        brokenlinksflag = 1
        brokenlinks = articlelinks[j] + ' ' + format(inst) + ', ' + brokenlinks
        continue
    except SocketError as inst:
        brokenlinksflag = 1
        brokenlinks = articlelinks[j] + ' ' + sys.exc_info()[0] + ', ' + brokenlinks
        continue
    except:
        print("This article killed everything: " + articlelinks[j])
        exit()
  File "exceptionerror.py", line 97, in <module>
    raw_response = response.read().decode('utf8', errors='ignore')
try:
    raw_response = response.read().decode('utf8', errors='ignore')
except ConnectionResetError:
    brokenlinksflag = 1
    brokenlinks = articlelinks[j] + ' ConnectionResetError, ' + brokenlinks
    continue