python-binance icon indicating copy to clipboard operation
python-binance copied to clipboard

Websocket does not connect

Open salian opened this issue 4 years ago • 1 comments

Describe the bug

Even with a very basic example, I could not get the ThreadedWebsocketManager to connect, due to failing CONNECTING WebSocket connection with code 1006. (Logs below).

However I was able to connect to the websocket API via other code (a different node.js project) using the same credentials on the same machine. Additionally, a query using Postman, from the same network, to /sapi/v1/account/status does not show any problems.

{
    "data": "Normal"
}

To Reproduce Code snippet to reproduce the behavior: I used this basic example but didn't manage to get it to work.

from binance import ThreadedWebsocketManager
import logging
from secrets import BINANCE_CREDENTIALS


def main():

    symbol = 'BNBBTC'

    twm = ThreadedWebsocketManager(api_key=api_key, api_secret=api_secret)
    twm.start()

    def handle_socket_message(msg):
        print(f"message type: {msg['e']}")
        print(msg)

    twm.start_kline_socket(callback=handle_socket_message, symbol=symbol)
    twm.join()


if __name__ == "__main__":
    logger = logging.getLogger('websockets')
    logger.setLevel(logging.DEBUG)
    logger.addHandler(logging.StreamHandler())
    api_key = BINANCE_CREDENTIALS['api_key']
    api_secret = BINANCE_CREDENTIALS['api_secret']

    main()

Expected behavior A clear and concise description of what you expected to happen.

I expect a connection to be established and to receive intermittent messages over the websocket.

Environment (please complete the following information):

  • Python version: Same issue was seen on 3.8 and 3.9
  • Virtual Env: virtualenv
  • OS: MacOS 10.15.6
  • python-binance version python-binance==1.0.12, websockets==9.1

Logs or Additional context

client - state = CONNECTING
client - event = connection_made(<asyncio.sslproto._SSLProtocolTransport object at 0x7fca231f4d00>)
client ! failing CONNECTING WebSocket connection with code 1006
client x closing TCP connection
client ! timed out waiting for TCP close
client x aborting TCP connection
client - event = connection_lost(None)
client - state = CLOSED
client x code = 1006, reason = [no reason]
client - state = CONNECTING
DEBUG:websockets.protocol:client - state = CONNECTING
client - event = connection_made(<asyncio.sslproto._SSLProtocolTransport object at 0x7fca231f5be0>)
DEBUG:websockets.protocol:client - event = connection_made(<asyncio.sslproto._SSLProtocolTransport object at 0x7fca231f5be0>)
client ! failing CONNECTING WebSocket connection with code 1006
DEBUG:websockets.protocol:client ! failing CONNECTING WebSocket connection with code 1006
client x closing TCP connection
DEBUG:websockets.protocol:client x closing TCP connection
client ! timed out waiting for TCP close
DEBUG:websockets.protocol:client ! timed out waiting for TCP close
client x aborting TCP connection
DEBUG:websockets.protocol:client x aborting TCP connection
client - event = connection_lost(None)
DEBUG:websockets.protocol:client - event = connection_lost(None)
client - state = CLOSED
DEBUG:websockets.protocol:client - state = CLOSED
client x code = 1006, reason = [no reason]
DEBUG:websockets.protocol:client x code = 1006, reason = [no reason]

salian avatar Jun 17 '21 12:06 salian

I just want to share updating Python to the most recent version fixed my issue. The same issue described above.

djdino56 avatar Jun 23 '22 18:06 djdino56