oliver-zehentleitner / unicorn-binance-websocket-api

A Python SDK to use the Binance Websocket API`s (com+testnet, com-margin+testnet, com-isolated_margin+testnet, com-futures+testnet, com-coin_futures, us, tr, dex/chain+testnet) in a simple, fast, flexible, robust and fully-featured way.

Home Page:https://oliver-zehentleitner.github.io/unicorn-binance-websocket-api

Repository from Github https://github.comoliver-zehentleitner/unicorn-binance-websocket-apiRepository from Github https://github.comoliver-zehentleitner/unicorn-binance-websocket-api

userData websocket http error with proxy

Rogary opened this issue · comments

Version of this library.

unicorn_fy: 0.12.2
unicorn_binance_local_depth_cache: not found
unicorn_binance_rest_api: 1.10.0
unicorn_binance_trailing_stop_loss: not found
unicorn_binance_websocket_api: 1.46.1

Solution to Issue cannot be found in the documentation or other Issues and also occurs in the latest version of this library.

  • I checked the documentation and other Issues. I am using the latest version of this library.

Hardware?

Local server/workstation

Operating System?

macOS

Python version?

Python3.10

Installed packages

# packages in environment at /Users/rogary/Desktop/GridTrading/envs/unicorn-binance-websocket-api/env:
#
# Name                    Version                   Build  Channel
aiodns                    3.0.0                    pypi_0    pypi
aiohttp                   3.8.4                    pypi_0    pypi
aiosignal                 1.3.1                    pypi_0    pypi
aniso8601                 9.0.1              pyhd8ed1ab_0    conda-forge
async-timeout             4.0.2                    pypi_0    pypi
attrs                     23.1.0                   pypi_0    pypi
blas                      1.0                    openblas  
blinker                   1.6.2              pyhd8ed1ab_0    conda-forge
boltons                   23.0.0          py311hca03da5_0  
brotli                    1.0.9                h1a8c8d9_8    conda-forge
brotli-bin                1.0.9                h1a8c8d9_8    conda-forge
brotli-python             1.0.9           py311h313beb8_7  
bzip2                     1.0.8                h3422bc3_4    conda-forge
ca-certificates           2023.7.22            hf0a4a13_0    conda-forge
ccxt                      3.1.46                   pypi_0    pypi
certifi                   2023.7.22          pyhd8ed1ab_0    conda-forge
cffi                      1.15.1          py311h80987f9_3  
chardet                   4.0.0           py311hca03da5_1003  
charset-normalizer        3.1.0              pyhd8ed1ab_0    conda-forge
cheroot                   10.0.0             pyhd8ed1ab_0    conda-forge
click                     8.1.3           unix_pyhd8ed1ab_2    conda-forge
colorama                  0.4.6              pyhd8ed1ab_0    conda-forge
conda                     23.7.2          py311hca03da5_0  
conda-package-handling    2.1.0           py311hca03da5_0  
conda-package-streaming   0.8.0           py311hca03da5_0  
cryptography              41.0.1                   pypi_0    pypi
dateparser                1.1.8                    pypi_0    pypi
dbutils                   3.0.3                    pypi_0    pypi
flask                     2.3.2              pyhd8ed1ab_0    conda-forge
flask-restful             0.3.10             pyhd8ed1ab_0    conda-forge
frozenlist                1.3.3                    pypi_0    pypi
idna                      3.4                pyhd8ed1ab_0    conda-forge
importlib-metadata        6.7.0              pyha770c72_0    conda-forge
itsdangerous              2.1.2              pyhd8ed1ab_0    conda-forge
jaraco-functools          3.7.0                    pypi_0    pypi
jaraco.functools          3.6.0              pyhd8ed1ab_0    conda-forge
jinja2                    3.1.2              pyhd8ed1ab_1    conda-forge
jsonpatch                 1.32               pyhd3eb1b0_0  
jsonpointer               2.1                pyhd3eb1b0_0  
libbrotlicommon           1.0.9                h1a8c8d9_8    conda-forge
libbrotlidec              1.0.9                h1a8c8d9_8    conda-forge
libbrotlienc              1.0.9                h1a8c8d9_8    conda-forge
libcxx                    16.0.6               h4653b0c_0    conda-forge
libffi                    3.4.2                h3422bc3_5    conda-forge
libgfortran               5.0.0           11_3_0_hca03da5_28  
libgfortran5              11.3.0              h009349e_28  
libopenblas               0.3.21               h269037a_0  
libsqlite                 3.42.0               hb31c410_0    conda-forge
libzlib                   1.2.13               h53f4e23_5    conda-forge
llvm-openmp               14.0.6               hc6e5704_0  
markupsafe                2.1.3                    pypi_0    pypi
more-itertools            9.1.0              pyhd8ed1ab_0    conda-forge
multidict                 6.0.4                    pypi_0    pypi
ncurses                   6.4                  h7ea286d_0    conda-forge
numpy                     1.24.3          py311hb57d4eb_0  
numpy-base                1.24.3          py311h1d85a46_0  
openssl                   3.1.1                h53f4e23_1    conda-forge
orjson                    3.8.8           py311ha6e5c4f_0  
packaging                 23.0            py311hca03da5_0  
pip                       23.1.2          py311hca03da5_0  
pluggy                    1.0.0           py311hca03da5_1  
psutil                    5.9.5                    pypi_0    pypi
pyasn1                    0.5.0                    pypi_0    pypi
pyasn1-modules            0.3.0                    pypi_0    pypi
pycares                   4.3.0                    pypi_0    pypi
pycosat                   0.6.4           py311h80987f9_0  
pycparser                 2.21               pyhd3eb1b0_0  
pymysql                   1.0.2           py311hca03da5_1  
pyopenssl                 23.2.0          py311hca03da5_0  
pysocks                   1.7.1              pyha2e5f31_6    conda-forge
python                    3.11.3               hb885b13_1  
python-dateutil           2.8.2              pyhd8ed1ab_0    conda-forge
pytz                      2023.3             pyhd8ed1ab_0    conda-forge
readline                  8.2                  h92ec313_1    conda-forge
regex                     2023.6.3                 pypi_0    pypi
requests                  2.31.0             pyhd8ed1ab_0    conda-forge
ruamel.yaml               0.17.21         py311h80987f9_0  
schedule                  1.2.0                    pypi_0    pypi
service-identity          23.1.0                   pypi_0    pypi
setuptools                67.7.2             pyhd8ed1ab_0    conda-forge
six                       1.16.0             pyh6c4a22f_0    conda-forge
sqlite                    3.42.0               h203b68d_0    conda-forge
tk                        8.6.12               he1e0b03_0    conda-forge
toolz                     0.12.0          py311hca03da5_0  
tqdm                      4.65.0          py311hb6e6a13_0  
typing-extensions         4.6.3                    pypi_0    pypi
tzdata                    2023c                h71feb2d_0    conda-forge
tzlocal                   5.0.1                    pypi_0    pypi
ujson                     5.8.0                    pypi_0    pypi
unicorn-binance-rest-api  1.10.0                   pypi_0    pypi
unicorn-binance-websocket-api 1.43.3             pyhd8ed1ab_0    conda-forge
unicorn-fy                0.12.2             pyhd8ed1ab_0    conda-forge
urllib3                   2.0.3           py311hca03da5_0  
websocket-client          1.6.0              pyhd8ed1ab_0    conda-forge
websockets                10.4            py311h80987f9_1  
werkzeug                  2.3.6              pyhd8ed1ab_0    conda-forge
wheel                     0.40.0             pyhd8ed1ab_0    conda-forge
xz                        5.4.2                h80987f9_0  
yarl                      1.9.2                    pypi_0    pypi
zipp                      3.15.0             pyhd8ed1ab_0    conda-forge
zlib                      1.2.13               h53f4e23_5    conda-forge
zstandard                 0.19.0          py311h80987f9_0

Logging output

Exception in thread _create_stream_thread:  stream_id=a618feb8fa6b-85f7-bfdf-bc7a-3cd5b891, time=1690769265.806821:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/urllib3/connectionpool.py", line 467, in _make_request
    self._validate_conn(conn)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/urllib3/connectionpool.py", line 1092, in _validate_conn
    conn.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/urllib3/connection.py", line 642, in connect
    sock_and_verified = _ssl_wrap_socket_and_match_hostname(
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/urllib3/connection.py", line 783, in _ssl_wrap_socket_and_match_hostname
    ssl_sock = ssl_wrap_socket(
               ^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/urllib3/util/ssl_.py", line 469, in ssl_wrap_socket
    ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls, server_hostname)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/urllib3/util/ssl_.py", line 513, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ssl.py", line 517, in wrap_socket
    return self.sslsocket_class._create(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ssl.py", line 1075, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ssl.py", line 1346, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Hostname mismatch, certificate is not valid for 'api.binance.com'. (_ssl.c:1002)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/urllib3/connectionpool.py", line 790, in urlopen
    response = self._make_request(
               ^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/urllib3/connectionpool.py", line 491, in _make_request
    raise new_e
urllib3.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Hostname mismatch, certificate is not valid for 'api.binance.com'. (_ssl.c:1002)

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

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/requests/adapters.py", line 486, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/urllib3/connectionpool.py", line 844, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/urllib3/util/retry.py", line 515, in increment
    raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
urllib3.exceptions.MaxRetryError: SOCKSHTTPSConnectionPool(host='api.binance.com', port=443): Max retries exceeded with url: /api/v3/ping (Caused by SSLError(SSLCertVerificationError(1, "[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Hostname mismatch, certificate is not valid for 'api.binance.com'. (_ssl.c:1002)")))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/unicorn_binance_websocket_api/manager.py", line 569, in _create_stream_thread
    loop.run_until_complete(socket.start_socket())
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/unicorn_binance_websocket_api/sockets.py", line 66, in start_socket
    async with BinanceWebSocketApiConnection(self.manager,
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/unicorn_binance_websocket_api/connection.py", line 81, in __aenter__
    uri = self.manager.create_websocket_uri(self.channels,
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/unicorn_binance_websocket_api/manager.py", line 1502, in create_websocket_uri
    response = self.get_listen_key_from_restclient(stream_id, api_key, api_secret, symbols=symbols)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/unicorn_binance_websocket_api/manager.py", line 2083, in get_listen_key_from_restclient
    response = self.restclient.get_listen_key(stream_id)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/unicorn_binance_websocket_api/restclient.py", line 143, in get_listen_key
    ubra = BinanceRestApiManager(api_key=self.api_key, api_secret=self.api_secret,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/unicorn_binance_rest_api/manager.py", line 370, in __init__
    self.ping()
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/unicorn_binance_rest_api/manager.py", line 769, in ping
    return self._get('ping', version=self.PRIVATE_API_VERSION)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/unicorn_binance_rest_api/manager.py", line 542, in _get
    return self._request_api('get', path, signed, version, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/unicorn_binance_rest_api/manager.py", line 494, in _request_api
    return self._request(method, uri, signed, throw_exception=throw_exception, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/unicorn_binance_rest_api/manager.py", line 482, in _request
    self.response = getattr(self.session, method)(uri,
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/requests/sessions.py", line 602, in get
    return self.request("GET", url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/requests/sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/requests/sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/requests/adapters.py", line 517, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: SOCKSHTTPSConnectionPool(host='api.binance.com', port=443): Max retries exceeded with url: /api/v3/ping (Caused by SSLError(SSLCertVerificationError(1, "[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Hostname mismatch, certificate is not valid for 'api.binance.com'. (_ssl.c:1002)")))

Processing method?

process_stream_data

Used endpoint?

binance.com-futures

Issue

in my local server i must use proxy to connect binance
so i create 2 connect like this

# for userData 
            self.binance_websocket_api_manager =  BinanceWebSocketApiManager( debug= True ,
                                                                            throw_exception_if_unrepairable=True,
                                                                            socks5_proxy_server="127.0.0.1:7890",
                                                                            socks5_proxy_ssl_verification=False,
                                                                            ping_timeout_default=30,
                                                                            exchange="binance.com-futures")
# for MarketData
            self.binance_websocket_api_manager = BinanceWebSocketApiManager( debug= True ,
                                                                            socks5_proxy_server="127.0.0.1:7890",
                                                                            socks5_proxy_ssl_verification=False,
                                                                            ping_timeout_default=30,
                                                                            exchange="binance.com-futures")

market data connected and work good but userData connect excepted when creating

user websocket status:  {'exchange': 'binance.com-futures', 'stream_id': 'e0a91953f62f-5082-ee51-eb6e-c11ace90', 'recent_socket_id': UUID('e93336ac-4c9a-4e58-ba0f-f594aeb59501'), 'channels': ['arr'], 'markets': ['!userData'], 'stream_label': None, 'stream_buffer_name': False, 'stream_buffer_maxlen': None, 'symbols': False, 'output': 'raw_data', 'subscriptions': 0, 'payload': [], 'api': False, 'api_key': '*****', 'api_secret': '******', 'dex_user_address': False, 'ping_interval': 10, 'ping_timeout': 30, 'close_timeout': 1, 'status': 'starting', 'start_time': 1690768348.3490949, 'processed_receives_total': 0, 'receives_statistic_last_second': {'most_receives_per_second': 0, 'entries': {}}, 'seconds_to_last_heartbeat': None, 'last_heartbeat': None, 'stop_request': None, 'crash_request': None, 'kill_request': None, 'seconds_since_has_stopped': None, 'has_stopped': False, 'reconnects': 0, 'last_stream_signal': None, 'logged_reconnects': [], 'processed_transmitted_total': 0, 'last_static_ping_listen_key': 0, 'listen_key': False, 'listen_key_cache_time': 600, 'last_received_data_record': None, 'processed_receives_statistic': {}, 'transfer_rate_per_second': {'bytes': {}, 'speed': 0}, 'websocket_uri': None}

look like httpconnect proxy except

Using ccxt with proxy in the same process it's work good

            client = ccxt.binance({
                'secret': self.secret,
                'apiKey': self.apiKey,
                'proxies': {
                    'http': 'http://127.0.0.1:7890',
                    'https': 'http://127.0.0.1:7890',
                },
                })

Hello!

Sorry you had to wait so long for an answer!
I need to test and reproduce this. I will get back to you.

sorry, it works for me. can you please test it again with the current version?

You can chat with me directly in our web chat: https://www.lucit.tech/get-support.html

I'll close this, if the problem still exists, please just post here!