Can't run any websocket example on binance-connector-python on Windows

INFO:root:Start to connect....
WARNING:root: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))
ERROR:root:Lost connection to Server. Reason: [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionDone'>: Connection was closed cleanly.
]. Retrying: 2

The error message printed when trying to run websocket examples in binance connector python.

This is a known issue with pyOpenSSL package on windows. The bug is that twisted does not includes a trust root sets of its own. The workaround here is by using certifi and setting the correct environment variable for twisted manually. This is the API documentation about the environment variable(SSL_CERT_FILE) to set for twisted.

If you run the file from cmd,

pip install certifi
for /f %i in ('python -m certifi') do set SSL_CERT_FILE=%i
python example.py

if you run the file on an IDE e.g pycharm,

  1. pip install certifi
  2. Get the file path of cacert.pem by running python -m certifi
  3. Copy the file path of cacert.pem and set the Environment Variables for SSL_CERT_FILE. These are the steps to setup environment variable on windows.
  4. Restart IDE and run the websocket example

P.S: These steps were done by using PyCharm and PyCharm imports system environment variables automatically when running the project. If you are using other IDEs, please ensure that the project runs with the system’s environment variables.

The original github issue can be found here

2 Likes