【问题标题】:ConnectionResetError: [Errno 104] Connection reset by peer from specific website | Python3ConnectionResetError: [Errno 104] 来自特定网站的对等方重置连接 | Python3
【发布时间】:2020-05-15 22:50:36
【问题描述】:

我正在尝试检查链接是否损坏。为此,我从带有 while 循环的字典列表中发送一个元素(链接),并且我正在使用 urllib.request。目标是仅从列表中删除断开的链接。列表包含来自https://jamanetwork.com/ 的不同文章的链接,我希望能够下载现有的文章。

但是我收到 ConnectionResetError: [Errno 104] Connection reset by peer。 当我尝试测试来自https://jamanetwork.com/ 的链接以及https://jamanetwork.com/ 上的每个页面时,我只会收到该错误,但代码似乎适用于其他网站。

我的问题是:我在这里遗漏了什么还是服务器端的问题?

这是我的代码(python3):

import urllib.request

i = 0
while i < (len(dicts)):
  url = dicts[i]['link']
  try:
    with urllib.request.urlopen(url) as f:
    status = f.getcode()
    i += 1
 except:
    del dicts[i]

这是一个回溯:

https://jamanetwork.com/
---------------------------------------------------------------------------

ConnectionResetError                      Traceback (most recent call last)
<ipython-input-59-8d93b45dbd14> in <module>()
     22 print(url)
     23 
---> 24 with urllib.request.urlopen("https://jamanetwork.com/") as f:
     25   status = f.getcode()
     26   print(status)

12 frames
/usr/lib/python3.6/ssl.py in read(self, len, buffer)
    629         """
    630         if buffer is not None:
--> 631             v = self._sslobj.read(len, buffer)
    632         else:
    633             v = self._sslobj.read(len)

ConnectionResetError: [Errno 104] Connection reset by peer

任何建议都非常感谢,谢谢!

【问题讨论】:

  • 你有网络问题吗?
  • 不,我也使用 google colab,不知道这是否重要

标签: python python-3.x python-requests


【解决方案1】:

基于this answer。您无法解决服务器错误。但是,你可以处理它。

所以你无能为力,这是服务器的问题。 但是您可以使用 try .. except 块来处理该异常:

试试这个代码:

import urllib.request

i = 0
while i < (len(dicts)):
    url = dicts[i]['link']
    try:
        f = urllib.request.urlopen(url)
    except:
        del dicts[i]
    else:
        with f:
            status = f.getcode()
            i += 1

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-03
  • 2016-10-10
  • 2018-05-22
  • 2014-01-04
  • 2021-08-27
相关资源
最近更新 更多