繁体   English   中英

ConnectionResetError:[Errno 104]由对等Python 3重置连接

[英]ConnectionResetError: [Errno 104] Connection reset by peer python 3

我有一个查询REST API(Oanda)的程序,并且在整个开发过程中都运行良好(大约一个月或两个月)。 以下是我请求数据的方式:

import urllib.request
def getCandles( currency="EUR_USD", count=500, granularity="D"):

    headers = getHeader()
    base_url = 'https://api-fxtrade.oanda.com/'
    ins_url = 'v3/instruments/{}/candles/?count={}&price=M&granularity={}'.format( currency, count, granularity)
    url = base_url + ins_url
    req = urllib.request.Request(url, headers=headers)
    response = urllib.request.urlopen(req)
    response = json.loads( response.decode('utf-8') )
    return response['candles']

我收到以下错误:

Traceback (most recent call last):
  File "test_indicators.py", line 109, in <module>
    candles = getCandles()
  File "test_indicators.py", line 19, in getCandles
    response = urllib.request.urlopen(url)
  File "/home/adam/anaconda3/lib/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/home/adam/anaconda3/lib/python3.6/urllib/request.py", line 526, in open
    response = self._open(req, data)
  File "/home/adam/anaconda3/lib/python3.6/urllib/request.py", line 544, in _open
    '_open', req)
  File "/home/adam/anaconda3/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/home/adam/anaconda3/lib/python3.6/urllib/request.py", line 1361, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/home/adam/anaconda3/lib/python3.6/urllib/request.py", line 1321, in do_open
    r = h.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/ssl.py", line 1012, in recv_into
    return self.read(nbytes, buffer)
  File "/home/adam/anaconda3/lib/python3.6/ssl.py", line 874, in read
    return self._sslobj.read(len, buffer)
  File "/home/adam/anaconda3/lib/python3.6/ssl.py", line 631, in read
    v = self._sslobj.read(len, buffer)
ConnectionResetError: [Errno 104] Connection reset by peer

我已经搜索了相关的问题,但是找不到答案,因为其中很多正在使用套接字模块,而我正在使用请求。 我似乎确实得到了错误是我自己的印象,但是,在编写此问题时,我意识到如果我在搜索栏中查询https://www.oanda.com/account/ ,我将得到500,所以我假设这是返回给req的内容。 这是否表示我要查询的网站已阻止我?

我很好地处于请求限制(它们允许的20 / sec中的1秒)之内,但是昨晚我确实超出了每个请求的数据点数限制,直到发现最大值为5000。 但是今天早上我运行它时,它运行良好。

另外,我并没有欺骗我的请求,因为以前的SO答复是请求的,并且感觉不需要,因为它是API,并且运行良好。 此外,我在Oanda重置了令牌授权,但仍然没有运气。 我想我的问题是,这听起来像是我被阻止了吗,或者这是我可以解决的问题,还是Oanda服务器刚刚关闭?

如果有帮助,我正在linux 16.02上使用Django 2.0和python 3。

谢谢,如果我没有遵循SO准则,我深表歉意,因为这是我很久以来第一个提出的问题。

我建议您使用python中的请求库。您可以在此处阅读http://docs.python-requests.org/en/master/ 其次,我尝试对您提到的URL进行获取请求,并得到401 - UnAuthorized作为响应。 原因是我没有标题。 如果您愿意,可以使用我为您修改的以下代码。

import requests
def getCandles( currency="EUR_USD", count=500, granularity="D"):

    headers = getHeader()
    base_url = 'https://api-fxtrade.oanda.com/'
    ins_url = 'v3/instruments/{}/candles/?count={}&price=M&granularity={}'.format( currency, count, granularity)
    url = base_url + ins_url
    response = requests.get(url=url, headers=headers)
    if response.status_code == 200:
       return response.json().get('candles')
    else:
        return None

另一件事应该做的是检查状态200。

暂无
暂无

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

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