简体   繁体   English

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

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

I have a program that queries a REST API (Oanda) and it has been working fine throughout development (about a month or two). 我有一个查询REST API(Oanda)的程序,并且在整个开发过程中都运行良好(大约一个月或两个月)。 The following is how I am requesting my data: 以下是我请求数据的方式:

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']

I get the following error: 我收到以下错误:

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

I've searched the related questions but couldn't find an answer as a lot of them are using socket module and I am using request. 我已经搜索了相关的问题,但是找不到答案,因为其中很多正在使用套接字模块,而我正在使用请求。 I did seem to get the impression the error was something on my side, however, in writing this question I realized if I query https://www.oanda.com/account/ in my search bar I get a 500, so I am assuming that is what is returned to req. 我似乎确实得到了错误是我自己的印象,但是,在编写此问题时,我意识到如果我在搜索栏中查询https://www.oanda.com/account/ ,我将得到500,所以我假设这是返回给req的内容。 Does this mean the site I am querying has blocked me? 这是否表示我要查询的网站已阻止我?

I am well under the request limits (1/sec of the 20/sec they allow), but last night I did exceed the limit of number of data points per request until I found 5000 was the maximum. 我很好地处于请求限制(它们允许的20 / sec中的1秒)之内,但是昨晚我确实超出了每个请求的数据点数限制,直到发现最大值为5000。 But it worked fine when I ran it this morning. 但是今天早上我运行它时,它运行良好。

Also, I am not spoofing my request as a previous SO answer requested and don't feel I need to as it's an API and has been working fine. 另外,我并没有欺骗我的请求,因为以前的SO答复是请求的,并且感觉不需要,因为它是API,并且运行良好。 In addition, I reset my token authorization on Oanda but still no luck. 此外,我在Oanda重置了令牌授权,但仍然没有运气。 I guess my question is if this sounds like I have been blocked or is this something I can fix on my end or is it possible Oanda server is just down? 我想我的问题是,这听起来像是我被阻止了吗,或者这是我可以解决的问题,还是Oanda服务器刚刚关闭?

If it helps I am using Django 2.0 and python 3 on linux 16.02. 如果有帮助,我正在linux 16.02上使用Django 2.0和python 3。

Thanks, and apologies if I didn't follow SO guidelines as this is my first question asked in quite awhile. 谢谢,如果我没有遵循SO准则,我深表歉意,因为这是我很久以来第一个提出的问题。

I would suggest you to use requests library in python.For which you can read over here http://docs.python-requests.org/en/master/ . 我建议您使用python中的请求库。您可以在此处阅读http://docs.python-requests.org/en/master/ Secondly I tried to make a get request to the URL you had mentioned and i got a 401 - UnAuthorized as the response. 其次,我尝试对您提到的URL进行获取请求,并得到401 - UnAuthorized作为响应。 The reason being that I do not have the headers. 原因是我没有标题。 If you want you can use the following piece of code i modified for you. 如果您愿意,可以使用我为您修改的以下代码。

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

Another thing which can should do is check for the status 200. 另一件事应该做的是检查状态200。

暂无
暂无

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

相关问题 connectionreseterror: (errno 104) 连接被对等方重置 - connectionreseterror: (errno 104) connection reset by peer Python套接字ConnectionResetError:[Errno 54]对等方与socket.error的连接重置:[Errno 104]对等方重置连接 - Python socket ConnectionResetError: [Errno 54] Connection reset by peer vs socket.error: [Errno 104] Connection reset by peer Google colab 下载 - ConnectionResetError: [Errno 104] Connection reset by peer - Google colab download - ConnectionResetError: [Errno 104] Connection reset by peer 如何修复“ConnectionResetError: [Errno 104] Connection reset by peer”错误? - How to fix "ConnectionResetError: [Errno 104] Connection reset by peer" error? python3.8 http.server - ConnectionResetError: [Errno 104] 对等方重置连接 - python3.8 http.server - ConnectionResetError: [Errno 104] Connection reset by peer ConnectionResetError:[Errno 104] 连接被特定网站的对等方重置 | Python3 - ConnectionResetError: [Errno 104] Connection reset by peer from specific website | Python3 在Python中由对等[Errno 104]重置连接 - Connection reset by peer [Errno 104] in Python 在Python 2.7中由peer [errno 104]重置连接 - Connection reset by peer [errno 104] in Python 2.7 ConnectionResetError:[Errno 104]由对等方和ERR_NAME_NOT_RESOLVED在heroku上重置连接,并通过Selenium进行了移动测试 - ConnectionResetError: [Errno 104] Connection reset by peer and ERR_NAME_NOT_RESOLVED on heroku with mobile testing through Selenium Python socket.error:[Errno 104]由peer重置连接 - Python socket.error: [Errno 104] Connection reset by peer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM