Somebody help! Connection never succeeded

I ran examples in the official Binance API python package. But the websocket examples failed to connect to the server. The error is like
"2022-04-28 05:09:02.115 UTC INFO binance.websocket.binance_client_factory: Start to connect…
2022-04-28 05:09:02.775 UTC WARNING binance.websocket.binance_client_protocol: WebSocket connection closed: connection was closed uncleanly (SSL error: unregistered scheme (in )), code: 1006, clean: False, reason: connection was closed uncleanly (SSL error: unregistered scheme (in ))
2022-04-28 05:09:02.776 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: 3
2022-04-28 05:09:03.723 UTC DEBUG root: closing ws connection"

I also could not link to the server by a simple url like wss://stream.binance.com:9443/ws/bnbbtc@depth

What’s the problem here?

With further investigation I found out that it is a certificate path issue under Windows.

In Powershell basically run:

PS C:\> $env:SSL_CERT_FILE=(py -m certifi); py yourscript.py

If necessary:

PS C:\> pip install certifi

Or in a script:

import os, certifi, win32api
from binance.websocket.spot.websocket_client \
    import SpotWebsocketClient as WebsocketClient
os.environ['SSL_CERT_FILE'] = certifi.where()
ws_client = WebsocketClient()


def message_handler(message):
    print(message)


ws_client.start()

ws_client.mini_ticker(
    symbol='bnbusdt',
    id=1,
    callback=message_handler,
)

# Combine selected streams
ws_client.instant_subscribe(
    stream=['bnbusdt@bookTicker', 'ethusdt@bookTicker'],
    callback=message_handler,
)

win32api.SetConsoleCtrlHandler(lambda _: ws_client.stop(), True)

I added the winapi controlhandler because i have no Break key and this works with Ctrl-C.
You can use the Break/Pause key instead (Ctrl-Pause or Ctrl-ScrLk in the osk)

See this link for other solutions
(cmd, different IDEs, how to set environment variable permanently):

Are you by any chace on a windows machine?
I have the same error message, but when I run it through WSL,
i.e. via the Kali Linux distribution, it works.

You can install any distro like Ubuntu, Suse, Debian via the Microsoft store.

Run it like this without logging in in your current directory:

kali run 'python yourscript.py'

Both, my Windows 11 and WSL Kali Linux have Python 3.10.5, Twisted 22.4.0 and websockets 10.3.

1 Like

It can be the networking issue from client side, try to connect to this url by websocat or other websocket client tool.

I tried several online websocket test tool. I can reach other websocket servers but cannot connect to Binance’s. I use ExpressVPN for all networks. Does that matter?

VPN can affect the connection, especially the websocket connection.

  1. testing with the same application from somewhere VPN is not required.
  2. Then try a different VPN from the same server

Then you should know where is the issue coming from.

1 Like