1

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!

Umut Gür
  • 369
  • 1
  • 4
  • 18

2 Answers2

0

This looks like an issue with your environment. My guess is that you have conflicting libraries which can impact any connection to the server.

I would propose a simple solution, by trying this I have successfully implemented your code and it works for me.

Output as proof:

{'result': None, 'id': 1}
{'e': 'kline', 'E': 1657403055883, 's': 'NEARUSDT', 'k': {'t': 1657403040000, 'T': 1657403099999, 's': 'NEARUSDT', 'i': '1m', 'f': 98639415, 'L': 98639419, 'o': '3.83500000', 'c': '3.83500000', 'h': '3.83500000', 'l': '3.83500000', 'v': '158.40000000', 'n': 5, 'x': False, 'q': '607.46400000', 'V': '158.40000000', 'Q': '607.46400000', 'B': '0'}}
2022-07-09 21:44:18.515 UTC INFO binance.websocket.binance_socket_manager: Connection with URL: wss://stream.binance.com:9443/ws
2022-07-09 21:44:18.579 UTC INFO binance.websocket.binance_client_factory: Start to connect....
{'e': 'kline', 'E': 1657403058583, 's': 'NEARUSDT', 'k': {'t': 1657403040000, 'T': 1657403099999, 's': 'NEARUSDT', 'i': '1m', 'f': 98639415, 'L': 98639420, 'o': '3.83500000', 'c': '3.83500000', 'h': '3.83500000', 'l': '3.83500000', 'v': '201.20000000', 'n': 6, 'x': False, 'q': '771.60200000', 'V': '201.20000000', 'Q': '771.60200000', 'B': '0'}}
2022-07-09 21:44:19.877 UTC INFO binance.websocket.binance_client_protocol: Server connected
2022-07-09 21:44:19.877 UTC INFO binance.websocket.binance_client_protocol: Sending message to Server: b'{"method": "SUBSCRIBE", "params": ["btcusdt@kline_3m"], "id": 2}'
{'result': None, 'id': 2}
{'e': 'kline', 'E': 1657403060304, 's': 'BTCUSDT', 'k': {'t': 1657402920000, 'T': 1657403099999, 's': 'BTCUSDT', 'i': '3m', 'f': 1447945972, 'L': 1447948128, 'o': '21711.68000000', 'c': '21709.64000000', 'h': '21717.58000000', 'l': '21705.02000000', 'v': '24.12056000', 'n': 2157, 'x': False, 'q': '523679.23670730', 'V': '11.76078000', 'Q': '255344.76392160', 'B': '0'}}
{'e': 'kline', 'E': 1657403061916, 's': 'NEARUSDT', 'k': {'t': 1657403040000, 'T': 1657403099999, 's': 'NEARUSDT', 'i': '1m', 'f': 98639415, 'L': 98639421, 'o': '3.83500000', 'c': '3.83500000', 'h': '3.83500000', 'l': '3.83500000', 'v': '204.50000000', 'n': 7, 'x': False, 'q': '784.25750000', 'V': '204.50000000', 'Q': '784.25750000', 'B': '0'}}

Steps to follow on your mac:

  1. Create a new directory mkdir some_directory
  2. create a requirements file with pip freeze > requirements.txt
  3. create a bin folder containing a .sh script for creating your virtual environment and installing libraries to the requirements file.

It should look like:

#some_directory/bin/setup_env.sh --- commented out

python -m venv venv
activate(){
    source ./venv/bin/activate
    echo "installing requirements.txt"
    pip install -r requirements.txt
}
activate

Then enter the following in your terminal . bin/setup_env.sh, then install the required libraries:

pip3 install binance-connector
pip3 install twisted

Then try running your script.

Working dollar
  • 306
  • 2
  • 10
0

Here is what worked for me:

  1. python -m certifi, just as you did
  2. used the full path name, starting with the drive
    SSL_CERT_FILE=C:/users/.../cacert.pem