繁体   English   中英

requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

[英]requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

我有这个讨厌的错误:

Traceback (most recent call last):
  File "/home/ubuntu/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 384, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "/home/ubuntu/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 380, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib/python3.5/http/client.py", line 1197, in getresponse
    response.begin()
  File "/usr/lib/python3.5/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.5/http/client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/lib/python3.5/socket.py", line 575, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [Errno 104] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ubuntu/.local/lib/python3.5/site-packages/requests/adapters.py", line 445, in send
    timeout=timeout
  File "/home/ubuntu/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/home/ubuntu/.local/lib/python3.5/site-packages/urllib3/util/retry.py", line 367, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/urllib3/packages/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 384, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "/home/ubuntu/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 380, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib/python3.5/http/client.py", line 1197, in getresponse
    response.begin()
  File "/usr/lib/python3.5/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.5/http/client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/lib/python3.5/socket.py", line 575, in readinto
    return self._sock.recv_into(b)
urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ubuntu/script.py", line 44, in <module>
    response_service_k = service_k_lib.service_kData(data, access_token, apifolder)
  File "/home/ubuntu/script_lib.py", line 238, in service_kData
    datarALL.extend(data.result())
  File "/usr/lib/python3.5/concurrent/futures/_base.py", line 398, in result
    return self.__get_result()
  File "/usr/lib/python3.5/concurrent/futures/_base.py", line 357, in __get_result
    raise self._exception
  File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/ubuntu/script_lib.py", line 129, in getdata3
    responsedata = requests.get(url, data=data, headers=hed, verify=False)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/requests/api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/requests/sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/requests/sessions.py", line 622, in send
    r = adapter.send(request, **kwargs)
  File "/home/ubuntu/.local/lib/python3.5/site-packages/requests/adapters.py", line 495, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

根据这个答案,我的问题的解决方案是在我的代码中策略性地放置: time.sleep(0.01) 我明白了,但我不确定我的代码中正确的位置在哪里:

这是来自script.py的相关代码部分:

result= []
with ThreadPoolExecutor(max_workers=num_of_pages) as executor:
    futh = [(executor.submit(self.getdata3, page, hed, data, apifolder,additional)) for page in pages]
    for data in as_completed(futh):
        result.extend(data.result())
print ("Finished generateing data.")
return result

这是getdata3函数的相关代码部分:

        def getdata3(...)
            datarALL = []
            responsedata = requests.get(url, data=data, headers=hed, verify=False)
            if responsedata.status_code == 200:  # 200 for successful call
                responsedata = responsedata.text
                jsondata = json.loads(responsedata)
                if "results" in jsondata:
                    if jsondata["results"]:
                        datarALL.extend(jsondata["results"])
            print ("{1} page {0} finished".format(page,str(datetime.now())))
            return datarALL

一些信息:

我的代码生成页面。

每页创建线程执行 getdata3(向 API 发出 GET 请求)

每个线程返回单个页面的结果。

“合并”结果。

我的问题:

time.sleep(0.01)放在哪里以避免此错误?

你可能已经想通了。 无论如何,我在 for 循环中发出 post 请求时遇到了类似的问题,该循环运行 400 次,每个循环运行两个请求,退出循环后的最后一个请求。 我在发出 API 请求时发现服务器 (check_mk) 尚未准备好,仍在编译一些热点和服务数据。 所以我把time.sleep()放在每个请求之前,这就解决了问题。 我不知道您多久提出一次请求,但我会这样尝试:

def getdata3(...)
    datarALL = []
    time.sleep(0.01)
    responsedata = requests.get(url, data=data, headers=hed, verify=False)
    if responsedata.status_code == 200:  # 200 for successful call
        responsedata = responsedata.text
        jsondata = json.loads(responsedata)
        if "results" in jsondata:
            if jsondata["results"]:
                datarALL.extend(jsondata["results"])
    print ("{1} page {0} finished".format(page,str(datetime.now())))
    return datarALL

正如发布问题的同一用户在此线程中所建议的那样,这可能是由于您的互联网连接。 我遇到了同样的问题,并将我的互联网连接更改为另一个网络,问题就为我解决了。 然后,您可以尝试更改您的互联网网络。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM