I am getting no response from the Binance api when executing the following code, is there anything that I could have missed here? do I need to create an account in order to get stream data through the api?
import json
import websocket
socket='wss://stream.binance.com:9443'
def on_open(ws):
print("opened")
subscribe_message = {
"method": "SUBSCRIBE",
"params":
[
"btcusdt@trade",
"btcusdt@depth"
],
"id": 1
}
ws.send(json.dumps(subscribe_message))
def on_message(ws, message):
print("received a message")
print(json.loads(message))
def on_close(ws):
print("closed connection")
ws = websocket.WebSocketApp(socket, on_open=on_open, on_message=on_message, on_close=on_close)
ws.run_forever()
In order to make your code work you just need to add /ws at the end of the websocket url:
socket = 'wss://stream.binance.com:9443/ws'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With