'SSL Certification Validation Failed in Binance Connector for Python

I am trying to build an app to detect pumps and dumps in crypto market, so I decided to use binance because of popularity.

So I installed binance-connector by pip3 install binance-connector on macOS 12.0.1 (I am using vs code as IDE)

So I tried to start with binance's examples here is the code;

import time
import logging
from binance.lib.utils import config_logging
from binance.websocket.spot.websocket_client import SpotWebsocketClient as Client

config_logging(logging, logging.DEBUG)


def message_handler(message):
    print(message)


my_client = Client()
my_client.start()

my_client.kline(symbol="nearusdt", id=1, interval="1m", callback=message_handler)

time.sleep(5)

my_client.kline(symbol="btcusdt", id=2, interval="3m", callback=message_handler)

time.sleep(10)

logging.debug("closing ws connection")
my_client.stop()

Here is output

admin@MacBook-Air CryptoTradeBot %  cd /Users/admin/Documents/GitHub/CryptoTradeBot ; /usr/bin/env /usr/local/bin/python3 /Users/admin/.vscode/extensions/ms-pytho
n.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/launcher 60230 -- /Users/admin/Documents/GitHub/CryptoTradeBot/main.py 
2022-03-07 19:05:50.237 UTC INFO binance.websocket.binance_socket_manager: Connection with URL: wss://stream.binance.com:9443/ws
2022-03-07 19:05:50.243 UTC INFO binance.websocket.binance_client_factory: Start to connect....
2022-03-07 19:05:51.055 UTC WARNING binance.websocket.binance_client_protocol: WebSocket connection closed: connection was closed uncleanly (SSL error: certificate verify failed (in tls_process_server_certificate)), code: 1006, clean: False, reason: connection was closed uncleanly (SSL error: certificate verify failed (in tls_process_server_certificate))
2022-03-07 19:05:51.056 UTC ERROR binance.websocket.binance_client_factory: Lost connection to Server. Reason: [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionDone'>: Connection was closed cleanly.
]. Retrying: 1
2022-03-07 19:05:53.881 UTC INFO binance.websocket.binance_client_factory: Start to connect....
2022-03-07 19:05:54.444 UTC WARNING binance.websocket.binance_client_protocol: WebSocket connection closed: connection was closed uncleanly (SSL error: certificate verify failed (in tls_process_server_certificate)), code: 1006, clean: False, reason: connection was closed uncleanly (SSL error: certificate verify failed (in tls_process_server_certificate))
2022-03-07 19:05:54.445 UTC ERROR binance.websocket.binance_client_factory: Lost connection to Server. Reason: [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionDone'>: Connection was closed cleanly.
]. Retrying: 2
2022-03-07 19:05:55.243 UTC INFO binance.websocket.binance_socket_manager: Connection with URL: wss://stream.binance.com:9443/ws
2022-03-07 19:05:55.250 UTC INFO binance.websocket.binance_client_factory: Start to connect....
2022-03-07 19:05:55.957 UTC WARNING binance.websocket.binance_client_protocol: WebSocket connection closed: connection was closed uncleanly (SSL error: certificate verify failed (in tls_process_server_certificate)), code: 1006, clean: False, reason: connection was closed uncleanly (SSL error: certificate verify failed (in tls_process_server_certificate))
2022-03-07 19:05:55.957 UTC ERROR binance.websocket.binance_client_factory: Lost connection to Server. Reason: [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionDone'>: Connection was closed cleanly.
]. Retrying: 1

There are a lot solution on web so here is what I've tried until now:

  1. Using python -m certifi output as SSL_CERT_FILE env value here is result of printenv command

... SSL_CERT_FILE=/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/certifi/cacert.pem ...

  1. Running Install Certificates.command

  2. Install OpenSSL with Homebrew

But still I am getting same error.

How can I resolve this? Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source