使用python请求时出现间歇性的“连接中止”错误

时间:2019-06-08 02:58:21

标签: python python-requests

过去几个月来,我一直在成功查询api,没有出现任何问题。今天,我得到一个错误,尽管代码没有更改。我过去曾在这里问过类似的问题。 ConnectionResetError: [Errno 104] Connection reset by peer python 3推荐的解决方案可以解决问题,直到今晚。我已将代码与错误消息一起分解到最低限度。关于如何解决它的任何建议和/或关于为什么查询并不总是可靠的任何见解将不胜感激。谢谢!


def getOaCandles():

    #build the main request
    headers = getHeader() #contains user name and token 

    url = 'https://apifxtrade.oanda.com/v3/instruments/EUR_USD/candles/'

    response = requests.get( url=url, headers=headers )
    if response.status_code == 200:
        candles = response.json().get( 'candles' )
        print( candles )

    else:
        print( '\nOanda response status:{}\n'.format(response.status_code ))
        return None

getOaCandles()

通常我会收到json对象的列表。但这是错误:

adam@adam-Inspiron-15-7579:~/forex$ python oandaTester.py 

Traceback (most recent call last):
  File "/home/adam/anaconda3/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 594, in urlopen
    chunked=chunked)
  File "/home/adam/anaconda3/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 391, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "/home/adam/anaconda3/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 387, in _make_request
    httplib_response = conn.getresponse()
  File "/home/adam/anaconda3/lib/python3.6/http/client.py", line 1331, in getresponse
    response.begin()
  File "/home/adam/anaconda3/lib/python3.6/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/home/adam/anaconda3/lib/python3.6/http/client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/home/adam/anaconda3/lib/python3.6/socket.py", line 586, in readinto
    return self._sock.recv_into(b)
  File "/home/adam/anaconda3/lib/python3.6/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 261, in recv_into
    raise SocketError(str(e))
OSError: (104, 'ECONNRESET')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/adam/anaconda3/lib/python3.6/site-packages/requests/adapters.py", line 423, in send
    timeout=timeout
  File "/home/adam/anaconda3/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 643, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/home/adam/anaconda3/lib/python3.6/site-packages/requests/packages/urllib3/util/retry.py", line 334, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/home/adam/anaconda3/lib/python3.6/site-packages/requests/packages/urllib3/packages/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/adam/anaconda3/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 594, in urlopen
    chunked=chunked)
  File "/home/adam/anaconda3/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 391, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "/home/adam/anaconda3/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 387, in _make_request
    httplib_response = conn.getresponse()
  File "/home/adam/anaconda3/lib/python3.6/http/client.py", line 1331, in getresponse
    response.begin()
  File "/home/adam/anaconda3/lib/python3.6/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/home/adam/anaconda3/lib/python3.6/http/client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/home/adam/anaconda3/lib/python3.6/socket.py", line 586, in readinto
    return self._sock.recv_into(b)
  File "/home/adam/anaconda3/lib/python3.6/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 261, in recv_into
    raise SocketError(str(e))
requests.packages.urllib3.exceptions.ProtocolError: ('Connection aborted.', OSError("(104, 'ECONNRESET')",))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "oandaTester.py", line 42, in <module>
    getOaCandles(bad_url=True)
  File "oandaTester.py", line 32, in getOaCandles
    response = requests.get( url=url, headers=headers )
  File "/home/adam/anaconda3/lib/python3.6/site-packages/requests/api.py", line 70, in get
    return request('get', url, params=params, **kwargs)
  File "/home/adam/anaconda3/lib/python3.6/site-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/adam/anaconda3/lib/python3.6/site-packages/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/adam/anaconda3/lib/python3.6/site-packages/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/home/adam/anaconda3/lib/python3.6/site-packages/requests/adapters.py", line 473, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', OSError("(104, 'ECONNRESET')",))```

0 个答案:

没有答案