LUCIT-Systems-and-Development / unicorn-binance-rest-api

A Python SDK by LUCIT to use the Binance REST API`s (com+testnet, com-margin+testnet, com-isolated_margin+testnet, com-futures+testnet, us, tr) in a simple, fast, flexible, robust and fully-featured way.
https://unicorn-binance-rest-api.docs.lucit.tech
Other
60 stars 17 forks source link

Getting Connection Reset Error #2

Closed PribhuPatel closed 3 years ago

PribhuPatel commented 3 years ago

I am trying to make continuous requests to Binance. I am not going out of limit requests(> 1200) per minute. But still getting this error. Even my IP is not banned. After the error I rerun the code, it is working fine for the next 5-6 hours every time. And I am running this on VM. So, it is not the case that my IP is banned and after changing the internet connection my IP is changed.

from unicorn_binance_rest_api.unicorn_binance_rest_api_manager import BinanceRestApiManager
from unicorn_binance_rest_api.unicorn_binance_rest_api_exceptions import BinanceAPIException
import time
import logging
from requests.exceptions import ConnectionError
from urllib3.exceptions import ProtocolError

api_key = "key"
api_secret = "secret"
ubra = BinanceRestApiManager(api_key, api_secret)

avail_count = 10
coins = [{'symbol': 'QTUMUSDT'},
         {'symbol': 'ADAUSDT'},
         {'symbol': 'XRPUSDT'},
         {'symbol': 'MIRUSDT'}]

def buy_coin():
    global avail_count
    for coin in coins:
            current_price = float(ubra.get_symbol_ticker(symbol=coin["symbol"])["price"])
            ## Doing something with price

while True:
    try:
        buy_coin()
        if avail_count == 0:
            time.sleep(3)
    except (ConnectionResetError, ConnectionError, ProtocolError) as e:
        print(e)
        ubra = BinanceRestApiManager(api_key, api_secret)

Here is the full error

Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\Binance\venv\lib\site-packages\urllib3\connectionpool.py", line 706, in urlopen
    chunked=chunked,
  File "C:\Users\Administrator\Desktop\Binance\venv\lib\site-packages\urllib3\connectionpool.py", line 445, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "C:\Users\Administrator\Desktop\Binance\venv\lib\site-packages\urllib3\connectionpool.py", line 440, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1336, in getresponse
    response.begin()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 306, in begin
    version, status, reason = self._read_status()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 267, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 1071, in recv_into
    return self.read(nbytes, buffer)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 929, in read
    return self._sslobj.read(len, buffer)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\Binance\venv\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "C:\Users\Administrator\Desktop\Binance\venv\lib\site-packages\urllib3\connectionpool.py", line 756, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "C:\Users\Administrator\Desktop\Binance\venv\lib\site-packages\urllib3\util\retry.py", line 532, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "C:\Users\Administrator\Desktop\Binance\venv\lib\site-packages\urllib3\packages\six.py", line 734, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\Administrator\Desktop\Binance\venv\lib\site-packages\urllib3\connectionpool.py", line 706, in urlopen
    chunked=chunked,
  File "C:\Users\Administrator\Desktop\Binance\venv\lib\site-packages\urllib3\connectionpool.py", line 445, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "C:\Users\Administrator\Desktop\Binance\venv\lib\site-packages\urllib3\connectionpool.py", line 440, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1336, in getresponse
    response.begin()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 306, in begin
    version, status, reason = self._read_status()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 267, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 1071, in recv_into
    return self.read(nbytes, buffer)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 929, in read
    return self._sslobj.read(len, buffer)
urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "binance.py", line 243, in <module>
    ubra = BinanceRestApiManager(api_key, api_secret)
  File "C:\Users\Administrator\Desktop\Binance\venv\lib\site-packages\unicorn_binance_rest_api\unicorn_binance_rest_api_manager.py", line 179, in __init__
    res = self.get_server_time()
  File "C:\Users\Administrator\Desktop\Binance\venv\lib\site-packages\unicorn_binance_rest_api\unicorn_binance_rest_api_manager.py", line 577, in get_server_time
    return self._get('time', version=self.PRIVATE_API_VERSION)
  File "C:\Users\Administrator\Desktop\Binance\venv\lib\site-packages\unicorn_binance_rest_api\unicorn_binance_rest_api_manager.py", line 339, in _get
    return self._request_api('get', path, signed, version, **kwargs)
  File "C:\Users\Administrator\Desktop\Binance\venv\lib\site-packages\unicorn_binance_rest_api\unicorn_binance_rest_api_manager.py", line 287, in _request_api
    return self._request(method, uri, signed, **kwargs)
  File "C:\Users\Administrator\Desktop\Binance\venv\lib\site-packages\unicorn_binance_rest_api\unicorn_binance_rest_api_manager.py", line 281, in _request
    self.response = getattr(self.session, method)(uri, **kwargs)
  File "C:\Users\Administrator\Desktop\Binance\venv\lib\site-packages\requests\sessions.py", line 555, in get
    return self.request('GET', url, **kwargs)
  File "C:\Users\Administrator\Desktop\Binance\venv\lib\site-packages\requests\sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\Administrator\Desktop\Binance\venv\lib\site-packages\requests\sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\Administrator\Desktop\Binance\venv\lib\site-packages\requests\adapters.py", line 498, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))
oliver-zehentleitner commented 3 years ago

Hello!

I am not sure about your problem, but I THINK that the exception handling is causing this error...

I dont see the output of print(e) which would be very interesting! Because for example if there is a ProtocollError which maybe doesnt close the connection, it would trigger a reconnect an then you trigger an connectionResetError because there is already an existing connection and you run into an exception loop....

Con A is established and throws an exception without closing Con A. The exception gets catched and opens a new Con B, but there is still Con A, so its closing Con A with a new exception which triggers the creation of Con C, but Con B is still alive ....

You know what i mean? I am not sure but i would investigate into this direction.

b4rlw commented 3 years ago

Perhaps this isn't the correct place to inquire about this, but if this repo is a fork of python-binance, this issue may be somewhat related to: https://github.com/sammchardy/python-binance/issues/389 At least the symptoms are similar in that: you are not exceeding limit requests, you are not banned, and the script runs fine for a while but fails with a ConnectionError that is hard to handle correctly.

oliver-zehentleitner commented 3 years ago

most of the comments in python-binance are regarding to websockets, not the rest api.

so the error is comming from different sources.... (websocket and requests)

oliver-zehentleitner commented 3 years ago

@PribhuPatel I am closing this, if you want investigate further, just answer :)