Forums

asynchronous ccxt cannot connect to exchanges

I am trying to asynchronous ccxt (from ccxt.async_support), but every time I run any asynchronous functions that, for example, fetches ticker data from an exchange, it causes an error like this in the Python console:

>>> asyncio.run(print_poloniex_ethbtc_ticker())
poloniex requires to release all resources with an explicit call to the .close() coroutine. If you are using the exchange instance with async coroutines, add `await exchange.close()` to your code into a place when you're done with the exchange and don't need the exchange instance anymore (at th
e end of your async coroutine).
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f27e8d7d0f0>
Traceback (most recent call last):
  File "/home/gsembwdvqjjrsqkhdosya/.local/lib/python3.10/site-packages/aiohttp/connector.py", line 1025, in _wrap_create_connection
    return await self._loop.create_connection(*args, **kwargs)
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1064, in create_connection
    raise exceptions[0]
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1049, in create_connection
    sock = await self._connect_sock(
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 960, in _connect_sock
    await self.sock_connect(sock, address)
  File "/usr/local/lib/python3.10/asyncio/selector_events.py", line 500, in sock_connect
    return await fut
  File "/usr/local/lib/python3.10/asyncio/selector_events.py", line 535, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
ConnectionRefusedError: [Errno 111] Connect call failed ('104.16.146.208', 443)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/home/gsembwdvqjjrsqkhdosya/.local/lib/python3.10/site-packages/ccxt/async_support/base/exchange.py", line 199, in fetch
    async with session_method(yarl.URL(url, encoded=True),
  File "/home/gsembwdvqjjrsqkhdosya/.local/lib/python3.10/site-packages/aiohttp/client.py", line 1197, in __aenter__
    self._resp = await self._coro
  File "/home/gsembwdvqjjrsqkhdosya/.local/lib/python3.10/site-packages/aiohttp/client.py", line 581, in _request
    conn = await self._connector.connect(
  File "/home/gsembwdvqjjrsqkhdosya/.local/lib/python3.10/site-packages/aiohttp/connector.py", line 544, in connect
    proto = await self._create_connection(req, traces, timeout)
  File "/home/gsembwdvqjjrsqkhdosya/.local/lib/python3.10/site-packages/aiohttp/connector.py", line 944, in _create_connection
    _, proto = await self._create_direct_connection(req, traces, timeout)
  File "/home/gsembwdvqjjrsqkhdosya/.local/lib/python3.10/site-packages/aiohttp/connector.py", line 1257, in _create_direct_connection
    raise last_exc
  File "/home/gsembwdvqjjrsqkhdosya/.local/lib/python3.10/site-packages/aiohttp/connector.py", line 1226, in _create_direct_connection
    transp, proto = await self._wrap_create_connection(
  File "/home/gsembwdvqjjrsqkhdosya/.local/lib/python3.10/site-packages/aiohttp/connector.py", line 1033, in _wrap_create_connection
    raise client_error(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host api.poloniex.com:443 ssl:default [Connect call failed ('104.16.146.208', 443)]

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

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "<stdin>", line 3, in print_poloniex_ethbtc_ticker
  File "/home/gsembwdvqjjrsqkhdosya/.local/lib/python3.10/site-packages/ccxt/async_support/poloniex.py", line 813, in fetch_ticker
    await self.load_markets()
  File "/home/gsembwdvqjjrsqkhdosya/.local/lib/python3.10/site-packages/ccxt/async_support/poloniex.py", line 478, in load_markets
    markets = await super(poloniex, self).load_markets(reload, params)
  File "/home/gsembwdvqjjrsqkhdosya/.local/lib/python3.10/site-packages/ccxt/async_support/base/exchange.py", line 276, in load_markets
    raise e
  File "/home/gsembwdvqjjrsqkhdosya/.local/lib/python3.10/site-packages/ccxt/async_support/base/exchange.py", line 272, in load_markets
    result = await self.markets_loading
  File "/home/gsembwdvqjjrsqkhdosya/.local/lib/python3.10/site-packages/ccxt/async_support/base/exchange.py", line 261, in load_markets_helper
    currencies = await self.fetch_currencies()
  File "/home/gsembwdvqjjrsqkhdosya/.local/lib/python3.10/site-packages/ccxt/async_support/poloniex.py", line 682, in fetch_currencies
    response = await self.publicGetCurrencies(self.extend(params, {'includeMultiChainCurrencies': True}))
  File "/home/gsembwdvqjjrsqkhdosya/.local/lib/python3.10/site-packages/ccxt/async_support/base/exchange.py", line 816, in request
    return await self.fetch2(path, api, method, params, headers, body, config)
  File "/home/gsembwdvqjjrsqkhdosya/.local/lib/python3.10/site-packages/ccxt/async_support/base/exchange.py", line 813, in fetch2
    return await self.fetch(request['url'], request['method'], request['headers'], request['body'])
  File "/home/gsembwdvqjjrsqkhdosya/.local/lib/python3.10/site-packages/ccxt/async_support/base/exchange.py", line 237, in fetch
    raise ExchangeNotAvailable(details) from e
ccxt.base.errors.ExchangeNotAvailable: poloniex GET https://api.poloniex.com/currencies?includeMultiChainCurrencies=true

However, running a similar function using normal ccxt does not return an error, and works normally. I am using the proxy from https://help.pythonanywhere.com/pages/403ForbiddenError/ for both asynchronous and normal ccxt, and the proxy configurations are the same for both of them. I used ccxt.poloniex({"proxies":{"http":"proxy.server:3128","https":"proxy.server:3128"}}) to test them.

Also, using the requests library or curl on the exchange works fine as well. Running the same function on my computer without proxy settings works fine.

Is the reason why asynchronous ccxt is unable to connect to the exchanges because of the proxy? If so, how do I fix that?

That "connect call failed" means that it's not using the proxy for some reason. Perhaps you could check with the developers. of the ccxt library to see if there's some difference in the way you need to configure the proxy when in async mode?

Ok, I checked the ccxt documentation and it was because I didn't configure it properly.

Instead of exchange = ccxt.poloniex({"proxies":{"http":"proxy.server:3128","https":"proxy.server:3128"}}) I was supposed to just modify the httpProxy attribute of the exchange. Now it works, thank you for your help!

Thanks for letting us know!