ConnectionResetError:[Errno 104]由对等python 3

时间:2019-02-23 06:36:55

标签: python

我有一个查询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,所以我假设那是返回给要求这是否表示我要查询的网站已阻止我?

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

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

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

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

1 个答案:

答案 0 :(得分:0)

我建议您使用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。