Skip to content

Instantly share code, notes, and snippets.

@nuriyevn
Created January 9, 2024 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuriyevn/7eafde5298b00f1311f68d040b852518 to your computer and use it in GitHub Desktop.
Save nuriyevn/7eafde5298b00f1311f68d040b852518 to your computer and use it in GitHub Desktop.
web.py
import time
import os
import websocket
import json
from dotenv import load_dotenv
load_dotenv()
api_key = os.environ.get('binance_api')
api_secret = os.environ.get('binance_secret')
def on_message(ws, message):
print(message)
def on_error(ws, error):
print(error)
def on_close(ws, close_status_code, close_msg):
print("### closed ###")
def on_open(ws):
print('Connection opened')
def main():
symbol = 'BTCFDUSD'
socket = f'wss://stream.binance.com:443/ws/{symbol}@trade'
ws = websocket.WebSocketApp(socket,
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever()
'''
twm = ThreadedWebsocketManager(api_key=api_key, api_secret=api_secret)
# start is required to initialise its internal loop
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)
# multiple sockets can be started
twm.start_depth_socket(callback=handle_socket_message, symbol=symbol)
twm.start_symbol_ticker_socket(callback=handle_socket_message, symbol=symbol)
# or a multiplex socket can be started like this
# see Binance docs for stream names
streams = ['bnbbtc@miniTicker', 'bnbbtc@bookTicker']
twm.start_multiplex_socket(callback=handle_socket_message, streams=streams)
twm.join()
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment