如何格式化 websocket 请求? [英] How do I format a websocket request?
问题描述
我正在尝试用 Python 创建一个应用程序,当狗狗币地址的余额发生变化时,它会为 GPIO 端口供电.我正在使用 websocket API here 和 这个 websocket 客户端.
I'm trying to create an application in Python that powers a GPIO port when the balance of a Dogecoin address changes. I'm using the websocket API here and this websocket client.
我的代码如下所示:
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()
这显然不是最终代码,但我只是想看看我是否能够连接到 websocket 并获得任何类型的响应.当我运行该代码时,它会因为请求中的冒号而引发错误.我不知道我应该如何格式化它才不会抛出错误.
It's obviously not the final code, but I just wanted to see if I'm even able to connect to the websocket and get any kind of response. When I run that code, it throws errors because of the colons in the request. I don't know what way I should format it that it won't throw an error.
推荐答案
我猜 API 需要 JSON 数据.你可以这样:
I'm guessing that the API wants JSON data. You can get that like so:
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()
这篇关于如何格式化 websocket 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!