簡體   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