gpt4 book ai didi

python - 用 Python 捕获 ConnectionResetError

转载 作者:太空狗 更新时间:2023-10-30 01:14:06 31 4
gpt4 key购买 nike

我正在构建一个 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 SocketError as inst:
brokenlinksflag = 1
brokenlinks = articlelinks[j] + ' ' + sys.exc_info()[0] + ', ' + brokenlinks
continue

和:

except ConnectionResetError as inst:
brokenlinksflag = 1
brokenlinks = articlelinks[j] + ' ' + sys.exc_info()[0] + ', ' + brokenlinks
continue

甚至是一个完整的通用异常来 try catch 所有错误,这样它就不会杀死整个脚本:

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,并且在尝试解码响应时产生错误,这意味着 try/except 条件围绕错误行。

以下解决了问题:

try:
raw_response = response.read().decode('utf8', errors='ignore')
except ConnectionResetError:
brokenlinksflag = 1
brokenlinks = articlelinks[j] + ' ConnectionResetError, ' + brokenlinks
continue

关于python - 用 Python 捕获 ConnectionResetError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32364674/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com