Python教程

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

本文主要是介绍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)))

这篇关于python3 通过websockets发送wss请求的例子的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!