import websockets
import asyncio
import json
from datetime import datetime
async def subscribe_spot():
ws_url = "wss://stream.binance.com:9443/ws"
symbols = ["ETHUSDT","BTCUSDT","ETHBTC","BTCUSDC"]
depth_level = "10"
subscribe_msg = {
"method": "SUBSCRIBE",
"params": [f"{symbol}@depth{depth_level}".lower() for symbol in symbols],
"id": 1111
}
while True:
try:
print(f"connecting WebSocket...")
async with websockets.connect(ws_url, ping_interval=20, ping_timeout=60) as ws:
await ws.send(json.dumps(subscribe_msg))
print(f"SUBSCRIBE {symbols} depth successful")
# 持续接收数据
async for message in ws:
data = json.loads(message)
print(data)
except (websockets.ConnectionClosed, asyncio.TimeoutError) as e:
print(f"连接断开: {e}, 5秒后重连...")
await asyncio.sleep(5)
except Exception as e:
print(f"未知错误: {e}, 10秒后重连...")
await asyncio.sleep(10)
if __name__ == "__main__":
asyncio.run(subscribe_spot())
look at the output data below, there is not a ‘symbol’ key or stream name to distinguish differ records…
{‘lastUpdateId’: 73585031507, ‘bids’: [[‘119030.09000000’, ‘2.65670000’], [‘119030.08000000’, ‘0.00005000’], [‘119029.11000000’, ‘0.00041000’], [‘119028.29000000’, ‘0.00010000’], [‘119028.28000000’, ‘0.27741000’], [‘119028.00000000’, ‘0.02020000’], [‘119027.74000000’, ‘0.00082000’], [‘119027.17000000’, ‘0.01942000’], [‘119026.82000000’, ‘0.00917000’], [‘119026.42000000’, ‘0.00005000’]], ‘asks’: [[‘119030.10000000’, ‘1.29831000’], [‘119030.53000000’, ‘0.00010000’], [‘119030.54000000’, ‘0.27746000’], [‘119030.71000000’, ‘0.00010000’], [‘119030.72000000’, ‘0.08336000’], [‘119031.57000000’, ‘0.00005000’], [‘119031.67000000’, ‘0.00010000’], [‘119031.68000000’, ‘0.10088000’], [‘119031.90000000’, ‘0.00005000’], [‘119032.40000000’, ‘0.00010000’]]}
{‘lastUpdateId’: 56264704904, ‘bids’: [[‘3903.84000000’, ‘52.32380000’], [‘3903.83000000’, ‘0.00140000’], [‘3903.82000000’, ‘0.00680000’], [‘3903.81000000’, ‘1.37250000’], [‘3903.80000000’, ‘4.50130000’], [‘3903.79000000’, ‘0.00140000’], [‘3903.78000000’, ‘0.00140000’], [‘3903.77000000’, ‘0.00280000’], [‘3903.75000000’, ‘0.00140000’], [‘3903.74000000’, ‘0.04590000’]], ‘asks’: [[‘3903.85000000’, ‘55.07200000’], [‘3903.86000000’, ‘0.00140000’], [‘3903.87000000’, ‘0.00140000’], [‘3903.88000000’, ‘0.00140000’], [‘3903.89000000’, ‘0.00140000’], [‘3903.90000000’, ‘0.00400000’], [‘3903.91000000’, ‘0.04000000’], [‘3903.92000000’, ‘0.00280000’], [‘3903.93000000’, ‘0.00400000’], [‘3903.94000000’, ‘0.00400000’]]}
{‘lastUpdateId’: 10231784897, ‘bids’: [[‘119063.98000000’, ‘0.68880000’], [‘119062.01000000’, ‘0.00005000’], [‘119062.00000000’, ‘0.04010000’], [‘119060.01000000’, ‘0.00005000’], [‘119060.00000000’, ‘0.04010000’], [‘119059.73000000’, ‘0.00005000’], [‘119059.72000000’, ‘0.12668000’], [‘119058.01000000’, ‘0.00005000’], [‘119058.00000000’, ‘0.04010000’], [‘119057.60000000’, ‘0.01679000’]], ‘asks’: [[‘119063.99000000’, ‘1.22523000’], [‘119064.00000000’, ‘0.04019000’], [‘119064.51000000’, ‘0.00005000’], [‘119064.52000000’, ‘0.35182000’], [‘119064.84000000’, ‘0.00005000’], [‘119064.85000000’, ‘1.00000000’], [‘119065.30000000’, ‘0.04206000’], [‘119065.82000000’, ‘0.00880000’], [‘119066.00000000’, ‘0.04010000’], [‘119066.89000000’, ‘0.04143000’]]}
{‘lastUpdateId’: 8279190489, ‘bids’: [[‘0.03280000’, ‘20.74920000’], [‘0.03279000’, ‘54.87310000’], [‘0.03278000’, ‘50.53730000’], [‘0.03277000’, ‘65.16420000’], [‘0.03276000’, ‘52.57650000’], [‘0.03275000’, ‘42.88620000’], [‘0.03274000’, ‘66.81770000’], [‘0.03273000’, ‘76.22230000’], [‘0.03272000’, ‘50.75850000’], [‘0.03271000’, ‘14.54440000’]], ‘asks’: [[‘0.03281000’, ‘46.29060000’], [‘0.03282000’, ‘62.21690000’], [‘0.03283000’, ‘42.53700000’], [‘0.03284000’, ‘68.22930000’], [‘0.03285000’, ‘97.58400000’], [‘0.03286000’, ‘83.79700000’], [‘0.03287000’, ‘37.23480000’], [‘0.03288000’, ‘33.82530000’], [‘0.03289000’, ‘20.28840000’], [‘0.03290000’, ‘39.61690000’]]}
{‘lastUpdateId’: 73585031723, ‘bids’: [[‘119030.09000000’, ‘2.65345000’], [‘119030.08000000’, ‘0.00005000’], [‘119029.11000000’, ‘0.00041000’], [‘119028.29000000’, ‘0.00010000’], [‘119028.28000000’, ‘0.27741000’], [‘119028.00000000’, ‘0.02020000’], [‘119027.74000000’, ‘0.00082000’], [‘119027.17000000’, ‘0.01942000’], [‘119026.82000000’, ‘0.00917000’], [‘119026.42000000’, ‘0.00005000’]], ‘asks’: [[‘119030.10000000’, ‘1.51598000’], [‘119030.53000000’, ‘0.00010000’], [‘119030.54000000’, ‘0.27746000’], [‘119030.69000000’, ‘0.00010000’], [‘119030.70000000’, ‘0.08336000’], [‘119031.57000000’, ‘0.00005000’], [‘119031.67000000’, ‘0.00010000’], [‘119031.68000000’, ‘0.10088000’], [‘119031.90000000’, ‘0.00005000’], [‘119032.40000000’, ‘0.00010000’]]}
{‘lastUpdateId’: 56264705055, ‘bids’: [[‘3903.84000000’, ‘50.58170000’], [‘3903.83000000’, ‘0.00140000’], [‘3903.82000000’, ‘0.00680000’], [‘3903.81000000’, ‘1.37250000’], [‘3903.80000000’, ‘4.50130000’], [‘3903.79000000’, ‘0.00140000’], [‘3903.78000000’, ‘0.00140000’], [‘3903.77000000’, ‘0.00280000’], [‘3903.76000000’, ‘0.00140000’], [‘3903.75000000’, ‘0.00140000’]], ‘asks’: [[‘3903.85000000’, ‘62.21880000’], [‘3903.86000000’, ‘0.00140000’], [‘3903.87000000’, ‘0.00140000’], [‘3903.88000000’, ‘0.00140000’], [‘3903.89000000’, ‘0.00140000’], [‘3903.90000000’, ‘0.00400000’], [‘3903.91000000’, ‘1.02740000’], [‘3903.92000000’, ‘0.00280000’], [‘3903.93000000’, ‘0.00400000’], [‘3903.94000000’, ‘0.00400000’]]}
{‘lastUpdateId’: 10231784939, ‘bids’: [[‘119063.98000000’, ‘0.67299000’], [‘119062.01000000’, ‘0.00005000’], [‘119062.00000000’, ‘0.04010000’], [‘119060.01000000’, ‘0.00005000’], [‘119060.00000000’, ‘0.04010000’], [‘119059.73000000’, ‘0.00005000’], [‘119059.72000000’, ‘0.12668000’], [‘119058.74000000’, ‘0.01679000’], [‘119058.01000000’, ‘0.00005000’], [‘119058.00000000’, ‘0.04010000’]], ‘asks’: [[‘119063.99000000’, ‘1.22523000’], [‘119064.00000000’, ‘0.04019000’], [‘119064.51000000’, ‘0.00005000’], [‘119064.52000000’, ‘0.35182000’], [‘119064.86000000’, ‘0.00005000’], [‘119064.87000000’, ‘1.00000000’], [‘119065.30000000’, ‘0.04206000’], [‘119065.82000000’, ‘0.00880000’], [‘119066.00000000’, ‘0.04010000’], [‘119066.89000000’, ‘0.04143000’]]}