#!/usr/bin/env python3
# coding=utf-8
import logging
import asyncio
import websockets
import time
import hmac
import hashlib
import base64
logging.basicConfig(
level=logging.INFO,format="%(asctime)s - %(levelname)s - %(message)s"
)
async def heartbeat(websocket: websockets.WebSocketClientProtocol):
while True:
message ='{"event":"ping"}'
await websocket.send(message)
logging.info("ws sent: %s", message)
await asyncio.sleep(10)
async def receive_message(websocket: websockets.WebSocketClientProtocol):
while True:
response =await websocket.recv()
logging.info("ws recv: %s", response)
async def subscribe_ticker(websocket: websockets.WebSocketClientProtocol):
message ='{"event":"subscribe","channel":["tickers"],"symbols":["TRX_USDT_PERP"]}'
await websocket.send(message)
logging.info("ws sent: %s", message)
async def subscribe_book(websocket: websockets.WebSocketClientProtocol):
message = (
'{"event":"subscribe","channel":["book"],"symbols":["TRX_USDT_PERP"],"depth":5}'
)
await websocket.send(message)
logging.info("ws sent: %s", message)
async def subscribe_booklv2(websocket: websockets.WebSocketClientProtocol):
message ='{"event":"subscribe","channel":["book_lv2"],"symbols":["TRX_USDT_PERP"]}'
await websocket.send(message)
logging.info("ws sent: %s", message)
async def subscribe_kline(websocket: websockets.WebSocketClientProtocol):
message ='{"event":"subscribe","channel":["candles_minute_1"],"symbols":["TRX_USDT_PERP"]}'
await websocket.send(message)
logging.info("ws sent: %s", message)
async def subscribe_trades(websocket: websockets.WebSocketClientProtocol):
message ='{"event":"subscribe","channel":["trades"],"symbols":["BTC_USDT_PERP"]}'
await websocket.send(message)
logging.info("ws sent: %s", message)
async def subscribe_index_price(websocket: websockets.WebSocketClientProtocol):
message ='{"event":"subscribe","channel":["index"],"symbols":["TRX_USDT_PERP"]}'
await websocket.send(message)
logging.info("ws sent: %s", message)
async def subscribe_index_candles_minute_1(
websocket: websockets.WebSocketClientProtocol,
):
message ='{"event":"subscribe","channel":["index_candles_minute_1"],"symbols":["TRX_USDT_PERP"]}'
await websocket.send(message)
logging.info("ws sent: %s", message)
async def subscribe_mark_price(websocket: websockets.WebSocketClientProtocol):
message = (
'{"event":"subscribe","channel":["mark_price"],"symbols":["TRX_USDT_PERP"]}'
)
await websocket.send(message)
logging.info("ws sent: %s", message)
async def subscribe_mark_price_candles_minute_1(
websocket: websockets.WebSocketClientProtocol,
):
message ='{"event":"subscribe","channel":["mark_price_candles_minute_1"],"symbols":["TRX_USDT_PERP"]}'
await websocket.send(message)
logging.info("ws sent: %s", message)
async def subscribe_funding_rate(websocket: websockets.WebSocketClientProtocol):
message = (
'{"event":"subscribe","channel":["funding_rate"],"symbols":["TRX_USDT_PERP"]}'
)
await websocket.send(message)
logging.info("ws sent: %s", message)
async def subscribe_account(websocket: websockets.WebSocketClientProtocol):
message ='{"event":"subscribe","channel":["account"],"symbols":["TRX_USDT_PERP"]}'
await websocket.send(message)
logging.info("ws sent: %s", message)
async def subscribe_orders(websocket: websockets.WebSocketClientProtocol):
message ='{"event":"subscribe","channel":["orders"],"symbols":["BTC_USDT_PERP"]}'
await websocket.send(message)
logging.info("ws sent: %s", message)
async def subscribe_positions(websocket: websockets.WebSocketClientProtocol):
message = (
'{"event":"subscribe","channel":["positions"],"symbols":["TRX_USDT_PERP"]}'
)
await websocket.send(message)
logging.info("ws sent: %s", message)
async def subscribe_trade(websocket: websockets.WebSocketClientProtocol):
message ='{"event":"subscribe","channel":["trade"],"symbols":["TRX_USDT_PERP"]}'
await websocket.send(message)
logging.info("ws sent: %s", message)
async def test_public():
uri ="ws://172.*.*.*:8090/ws/public"
async with websockets.connect(uri)as websocket:
await asyncio.gather(
heartbeat(websocket),# 这两个必须开着不能关
receive_message(websocket),# 这两个必须开着不能关
subscribe_trades(websocket),
# subscribe_booklv2(websocket),
# subscribe_funding_rate(websocket)
)
async def test_private():
uri ="ws://172.*.*.*:8090/ws/private"
key ="U5S7TXCL-V9NT30FS-6MYO63TV-O42O8ML4"
secret ="4e64b7e90557206e1c24bf4538b4cbaae500bb1593e3117cf3c18b9d936ae0bbeef4e5e7d795b2ecbce39b77c24d0028ff5504a23cf600d44adac6cbc6975344"
ts =round(time.time() *1000)
sign =f"GET\n/ws\nsignTimestamp={ts}"
h = hmac.new(secret.encode(), sign.encode(), hashlib.sha256)
digest = h.digest()
base64_result = base64.b64encode(digest).decode()
async with websockets.connect(uri)as websocket:
await websocket.send(
f"""{{
"event": "subscribe",
"channel": ["auth"],
"params": {{
"key": "{key}",
"signTimestamp": {ts},
"signatureMethod":"HmacSHA256",
"signatureVersion":"2",
"signature": "{base64_result}"
}}
}}"""
)
logging.info("ws auth result: %s",await websocket.recv())
await asyncio.gather(
heartbeat(websocket),# 这两个必须开着不能关
receive_message(websocket),# 这两个必须开着不能关
# subscribe_account(websocket),
subscribe_orders(websocket),
# subscribe_positions(websocket),
# subscribe_trade(websocket),
)
try:
asyncio.run(test_public())
# asyncio.run(test_private())
except KeyboardInterrupt:
logging.info("Interrupted")
# ng-quotation-gateway 共有频道172.*.18.*
# ng-private-ws 私有频道 172.*.1*.252
#不支持pytest



网友评论