6

我正在通过 AWS-EC2 上的 Binance api 从 Binance 获取 OHLCV 数据。但是有时(几天一次)错误“104,'ECONNRESET'”发生并且程序停止。

程序用Python3.7.3编写,运行在AWS-EC2上

代码如下。但是这段代码无法继续。

当发生任何 api 或连接错误时,我想继续运行程序。

我应该如何处理这个错误?

while True:

    ..............

    try :
        klines = client.get_historical_klines(ticker_symbol, Client.KLINE_INTERVAL_1MINUTE, _from_str, _until_str)

    except BinanceAPIException as e:
        print(e)
        print('Something went wrong. Error occured at %s. Wait for 1 hour.' % (datetime.datetime.now().astimezone(timezone('UTC'))))
        sleep(3600)
        continue

    ..............

所有错误按摩都如下。

Traceback(最近一次调用最后一次):文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/connectionpool.py”,第 600 行,在 urlopen chunked=分块)文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/connectionpool.py”,第 384 行,在 _make_request Six.raise_from(e, None)文件“”,第 2 行,在 raise_from 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/connectionpool.py”,第 380 行,在 _make_request httplib_response = conn.getresponse() 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/http/client.py”,第 1321 行,在 getresponse response.begin() 文件中“/ home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/http/client.py”,第 296 行,开始版本,状态,原因 = self._read_status() 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/http/client.py”,第 257 行,在 _read_status 行 = str(self.fp .readline(_MAXLINE + 1), "iso-8859-1") 文件 "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/socket.py",第 589 行,在 readinto返回 self._sock.recv_into(b) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py”,第 312 行,在recv_into 返回 self.recv_into(*args, **kwargs) 文件 "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py",第 302 行,在 recv_into 中引发 SocketError(str(e)) OSError: (104, 'ECONNRESET')py”,第 257 行,在 _read_status 行 = str(self.fp.readline(_MAXLINE + 1),“iso-8859-1”) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib /python3.7/socket.py”,第 589 行,在 readinto 中返回 self._sock.recv_into(b) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site- packages/urllib3/contrib/pyopenssl.py”,第 312 行,recv_into 返回 self.recv_into(*args, **kwargs) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3. 7/site-packages/urllib3/contrib/pyopenssl.py”,第 302 行,在 recv_into 中引发 SocketError(str(e)) OSError: (104, 'ECONNRESET')py”,第 257 行,在 _read_status 行 = str(self.fp.readline(_MAXLINE + 1),“iso-8859-1”) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib /python3.7/socket.py”,第 589 行,在 readinto 中返回 self._sock.recv_into(b) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site- packages/urllib3/contrib/pyopenssl.py”,第 312 行,recv_into 返回 self.recv_into(*args, **kwargs) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3. 7/site-packages/urllib3/contrib/pyopenssl.py”,第 302 行,在 recv_into 中引发 SocketError(str(e)) OSError: (104, 'ECONNRESET')在 readinto 中返回 self._sock.recv_into(b) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py”,第 312 行, 在 recv_into 返回 self.recv_into(*args, **kwargs) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py ",第 302 行,在 recv_into 中引发 SocketError(str(e)) OSError: (104, 'ECONNRESET')在 readinto 中返回 self._sock.recv_into(b) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py”,第 312 行, 在 recv_into 返回 self.recv_into(*args, **kwargs) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py ",第 302 行,在 recv_into 中引发 SocketError(str(e)) OSError: (104, 'ECONNRESET')经济重置')经济重置')

在处理上述异常的过程中,又出现了一个异常:

Traceback(最近一次调用最后一次):文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/requests/adapters.py”,第 449 行,发送超时 =超时文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/connectionpool.py”,第 638 行,在 urlopen _stacktrace=sys.exc_info()[2 ]) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/util/retry.py”,第 368 行,增量提高 6.reraise(类型(错误),错误,_stacktrace)文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/packages/six.py”,第685行,重新提出raise value.with_traceback(tb) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/connectionpool.py”,第 600 行,在 urlopen chunked=chunked) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/connectionpool.py”,第 384 行,在 _make_request Six.raise_from( e,无)文件“”,第 2 行,在 raise_from 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/connectionpool.py”,第 380 行,在 _make_request httplib_response = conn.getresponse() 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/http/client.py”,第 1321 行,在 getresponse response.begin( ) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/http/client.py”,第 296 行,在开始版本、状态、原因 = self._read_status() 文件中“ /home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/http/client.py”,第 257 行,在 _read_status 行 = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") 文件 "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/socket .py”,第 589 行,在 readinto 中返回 self._sock.recv_into(b) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/contrib/ pyopenssl.py”,第 312 行,在 recv_into 返回 self.recv_into(*args, **kwargs) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/ urllib3/contrib/pyopenssl.py",第 302 行,在 recv_into 中引发 SocketError(str(e)) urllib3.exceptions.ProtocolError: ('Connection aborted.', OSError("(104, 'ECONNRESET')"))在 readinto 中返回 self._sock.recv_into(b) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py”,第 312 行, 在 recv_into 返回 self.recv_into(*args, **kwargs) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py ",第 302 行,在 recv_into 中引发 SocketError(str(e)) urllib3.exceptions.ProtocolError: ('Connection aborted.', OSError("(104, 'ECONNRESET')"))在 readinto 中返回 self._sock.recv_into(b) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py”,第 312 行, 在 recv_into 返回 self.recv_into(*args, **kwargs) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py ",第 302 行,在 recv_into 中引发 SocketError(str(e)) urllib3.exceptions.ProtocolError: ('Connection aborted.', OSError("(104, 'ECONNRESET')"))第 302 行,在 recv_into raise SocketError(str(e)) urllib3.exceptions.ProtocolError: ('Connection aborted.', OSError("(104, 'ECONNRESET')"))第 302 行,在 recv_into raise SocketError(str(e)) urllib3.exceptions.ProtocolError: ('Connection aborted.', OSError("(104, 'ECONNRESET')"))

在处理上述异常的过程中,又出现了一个异常:

回溯(最近一次通话):文件“BTC_USDT_BINANCE_minutes.py”,第 54 行,klines = client.get_historical_klines(ticker_symbol, Client.KLINE_INTERVAL_1MINUTE, _from_str, _until_str) 文件“/home/ec2-user/.pyenv/versions/3.7 .3/lib/python3.7/site-packages/binance/client.py”,第 765 行,在 get_historical_klines first_valid_ts = self._get_earliest_valid_timestamp(symbol, interval) 文件“/home/ec2-user/.pyenv/versions/3.7 .3/lib/python3.7/site-packages/binance/client.py”,第 723 行,在 _get_earliest_valid_timestamp endTime=None 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3. 7/site-packages/binance/client.py”,第 705 行,在 get_klines 返回 self._get('klines', data=params) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib /python3.7/site-packages/binance/client.py”,第 207 行,在 _get return self._request_api('get', path, signed, version, **kwargs) 文件“/home/ec2-user/.pyenv/versions /3.7.3/lib/python3.7/site-packages/binance/client.py”,第 181 行,在 _request_api 返回 self._request(method, uri, signed, **kwargs) 文件“/home/ec2-user /.pyenv/versions/3.7.3/lib/python3.7/site-packages/binance/client.py",第 175 行,在 _request response = getattr(self.session, method)(uri, **kwargs) 文件中“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/requests/sessions.py”,第 546 行,在 get return self.request('GET', url, **kwargs)文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/requests/sessions.py”,第533行,在resp = self.send(准备,**send_kwargs) 文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/requests/sessions.py”,第 646 行,发送 r = adapter.send(请求,**kwargs)文件“/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/requests/adapters.py”,第 498 行,在发送中引发 ConnectionError(err , request=request) requests.exceptions.ConnectionError: ('Connection aborted.', OSError("(104, 'ECONNRESET')"))ConnectionError: ('Connection aborted.', OSError("(104, 'ECONNRESET')"))ConnectionError: ('Connection aborted.', OSError("(104, 'ECONNRESET')"))

4

1 回答 1

6

假设您正在使用python-binance模块,很可能会发生错误,因为您试图重用client经过很长时间(服务器超时)。您可以尝试重新创建第client一个:

...
client = Client(api_key, api_secret)
while True:
    ...
    try :
        klines = client.get_historical_klines(ticker_symbol, Client.KLINE_INTERVAL_1MINUTE, _from_str, _until_str)

    except BinanceAPIException as e:
        print(e)
        print('Something went wrong. Error occured at %s. Wait for 1 hour.' % (datetime.datetime.now().astimezone(timezone('UTC'))))
        sleep(3600)
        client = Client(api_key, api_secret)
        continue
...

原因是因为下面的[Client][2]使用requests Session意味着使用urllib3连接池。此连接池在超时后无法重用。

于 2019-06-12T04:52:12.890 回答