python3 通过websockets发送wss请求的例子

import asyncio
import websockets
import json

msg = \
{
  "method" : "SUBSCRIBE",
  "id" : 9344,
  "params" : "public/get_book_summary_by_currency",
  "params" : {
    "currency" : "BTC",
    "kind" : "future"
  }
}

async def call_api(msg):
   async with websockets.connect('wss://test.deribit.com/ws/api/v2') as websocket:
       await websocket.send(msg)
       while websocket.open:
           response = await websocket.recv()
           # do something with the response...
           print(response)

asyncio.get_event_loop().run_until_complete(call_api(json.dumps(msg)))

上一篇:WebSocket概述极其漏洞解析


下一篇:websockets的原理