sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
5.91k stars 2.19k forks source link

aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host api.binance.com:443 ssl:default [None] #955

Open JohnShiuMK opened 3 years ago

JohnShiuMK commented 3 years ago

Hi,

Thanks for developing and maintaining the python-binance. It's very useful.

Recently, I'm trying to develop a connection for listening the Binance USDM futures user data, via the ThreadedWebsocketManager.start_futures_socket. However, the disconnection happens quite often with the error below.

Could anyone help please?

Task exception was never retrieved future: <Task finished coro=<KeepAliveWebsocket._keepalive_socket() done, defined at /app/streams.py:247> exception=ClientConnectorError(ConnectionKey(host='fapi.binance.c$m', port=443, is_ssl=True, ssl=None, proxy=None, proxy_auth=None, proxy_headers_hash=8217749035264735374), ConnectionResetError())> Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/aiohttp/connector.py", line 969, in _wrap_create_connection return await self._loop.create_connection(*args, **kwargs) # type: ignore # noqa File "/usr/local/lib/python3.7/asyncio/base_events.py", line 981, in create_connection ssl_handshake_timeout=ssl_handshake_timeout) File "/usr/local/lib/python3.7/asyncio/base_events.py", line 1009, in _create_connection_transport await waiter ConnectionResetError

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/app/streams.py", line 248, in _keepalive_socket listen_key = await self._get_listen_key() File "/app/streams.py", line 239, in _get_listen_key listen_key = await self._client.futures_stream_get_listen_key() File "/usr/local/lib/python3.7/site-packages/binance/client.py", line 7421, in futures_stream_get_listen_key res = await self._request_futures_api('post', 'listenKey', signed=False, data={}) File "/usr/local/lib/python3.7/site-packages/binance/client.py", line 6519, in _request_futures_api
return await self._request(method, uri, signed, True, kwargs) File "/usr/local/lib/python3.7/site-packages/binance/client.py", line 6495, in _request async with getattr(self.session, method)(uri, kwargs) as response: File "/usr/local/lib/python3.7/site-packages/aiohttp/client.py", line 1117, in aenter self._resp = await self._coro File "/usr/local/lib/python3.7/site-packages/aiohttp/client.py", line 521, in _request req, traces=traces, timeout=real_timeout File "/usr/local/lib/python3.7/site-packages/aiohttp/connector.py", line 535, in connect proto = await self._create_connection(req, traces, timeout) File "/usr/local/lib/python3.7/site-packages/aiohttp/connector.py", line 892, in _createconnection , proto = await self._create_direct_connection(req, traces, timeout) File "/usr/local/lib/python3.7/site-packages/aiohttp/connector.py", line 1051, in _create_direct_connection raise last_exc File "/usr/local/lib/python3.7/site-packages/aiohttp/connector.py", line 1032, in _create_direct_connection client_error=client_error, File "/usr/local/lib/python3.7/site-packages/aiohttp/connector.py", line 975, in _wrap_create_connection raise client_error(req.connection_key, exc) from exc aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host fapi.binance.com:443 ssl:default [None] Task exception was never retrieved future: <Task finished coro=<KeepAliveWebsocket._keepalive_socket() done, defined at /app/streams.py:247> exception=ClientConnectorError(ConnectionKey(host='api.binance.com', port=443, is_ssl=True, ssl=None, proxy=None, proxy_auth=None, proxy_headers_hash=8217749035264735374), ConnectionResetError())> Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/aiohttp/connector.py", line 969, in _wrap_create_connection return await self._loop.create_connection(*args, **kwargs) # type: ignore # noqa File "/usr/local/lib/python3.7/asyncio/base_events.py", line 981, in create_connection ssl_handshake_timeout=ssl_handshake_timeout) File "/usr/local/lib/python3.7/asyncio/base_events.py", line 1009, in _create_connection_transport await waiter ConnectionResetError The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/app/streams.py", line 248, in _keepalive_socket listen_key = await self._get_listen_key() File "/app/streams.py", line 235, in _get_listen_key listen_key = await self._client.stream_get_listen_key() File "/usr/local/lib/python3.7/site-packages/binance/client.py", line 7026, in stream_get_listen_key res = await self._post('userDataStream', False, data={}) File "/usr/local/lib/python3.7/site-packages/binance/client.py", line 6554, in _post return await self._request_api('post', path, signed, version, kwargs) File "/usr/local/lib/python3.7/site-packages/binance/client.py", line 6514, in _request_api return await self._request(method, uri, signed, kwargs) File "/usr/local/lib/python3.7/site-packages/binance/client.py", line 6495, in _request async with getattr(self.session, method)(uri, **kwargs) as response: File "/usr/local/lib/python3.7/site-packages/aiohttp/client.py", line 1117, in aenter self._resp = await self._coro File "/usr/local/lib/python3.7/site-packages/aiohttp/client.py", line 521, in _request req, traces=traces, timeout=real_timeout File "/usr/local/lib/python3.7/site-packages/aiohttp/connector.py", line 535, in connect proto = await self._create_connection(req, traces, timeout) File "/usr/local/lib/python3.7/site-packages/aiohttp/connector.py", line 892, in _createconnection , proto = await self._create_direct_connection(req, traces, timeout) File "/usr/local/lib/python3.7/site-packages/aiohttp/connector.py", line 1051, in _create_direct_connection raise last_exc File "/usr/local/lib/python3.7/site-packages/aiohttp/connector.py", line 1032, in _create_direct_connection client_error=client_error, File "/usr/local/lib/python3.7/site-packages/aiohttp/connector.py", line 975, in _wrap_create_connection raise client_error(req.connection_key, exc) from exc aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host api.binance.com:443 ssl:default [None]

giladwa commented 3 years ago

Run into the same issue, would appreciate a fix for this one. It can be easily simulated by disconnecting the network.

JohnShiuMK commented 3 years ago

Hi, I tried modifying the _request function in the client.py (at line 6499) and it seems to fix the problem:

async def _request(self, method, uri: str, signed: bool, force_params: bool = False, **kwargs):
    kwargs = self._get_request_kwargs(method, signed, force_params, **kwargs)

    ### modification trial ###
    while True:
        test = getattr(self.session, method)(uri, **kwargs) 
        logging.info(f'connection status code: {test.status_code}') # but it doesn't print anything
        if test.status_code == 200:
            break
    ####################

    async with getattr(self.session, method)(uri, **kwargs) as response:
        self.response = response
        return await self._handle_response(response)

With those lines in the modification trial, the user data stream still works after I reconnecting the network. Without those, it doesn't. I tried to understand how it fixes the problem by inserting some logging.info in between, but I can't get any logging being printed on the console, maybe due to the threading, so I'm not 100% sure how it works.

Also, I don't have much knowledge on the Async syntax, so I'm not sure too whether it's a good way to fix.

It would be highly appreciated if someone could give me some guidance :)

Thanks,