Skip to content

Instantly share code, notes, and snippets.

@ankitshubham97
Created June 15, 2022 08:19
Show Gist options
  • Save ankitshubham97/4a5f8eac0d2c95e7c1c0e22f2033eee0 to your computer and use it in GitHub Desktop.
Save ankitshubham97/4a5f8eac0d2c95e7c1c0e22f2033eee0 to your computer and use it in GitHub Desktop.
Binance websocket server <-> Python websocket client
import json
import websocket
import datetime
def ws_trades():
socket = f'wss://stream.binance.com:9443/ws/ethusdt@trade'
def on_message(wsapp,message):
json_message = json.loads(message)
handle_trades(json_message)
def on_error(wsapp,error):
print(error)
wsapp = websocket.WebSocketApp(socket, on_message=on_message, on_error=on_error)
wsapp.run_forever()
def handle_trades(json_message):
print(json_message)
print("-----------------------")
ws_trades()
@ankitshubham97
Copy link
Author

Steps to execute:

  1. Install pip3 package websocket-client pip3 install websocket-client
  2. Run using python3 websocket-binance.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment