如何格式化websocket请求?

iSa*_*FTW 3 python websocket dogecoin-api

我正在尝试用Python创建一个应用程序,当Dogecoin地址的平衡发生变化时,它会为GPIO端口供电.我在这里使用websocket API 和这个 websocket客户端.

我的代码看起来像这样:

from websocket import create_connection
ws = create_connection("wss://ws.dogechain.info/inv")
ws.send("op":"addr_sub", "addr":"dogecoin_address")
result =  ws.recv()
print (result)
ws.close()
Run Code Online (Sandbox Code Playgroud)

这显然不是最终的代码,但我只想知道我是否能够连接到websocket并获得任何响应.当我运行该代码时,由于请求中的冒号,它会抛出错误.我不知道我应该以什么方式格式化它不会引发错误.

Way*_*ner 7

我猜这个API需要JSON数据.你可以这样:

import json
from websocket import create_connection
ws = create_connection("wss://ws.dogechain.info/inv")
ws.send(json.dumps({"op":"addr_sub", "addr":"dogecoin_address"}))
result =  ws.recv()
print (result)
ws.close()
Run Code Online (Sandbox Code Playgroud)