Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I format a websocket request?

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.

My code looks like this:

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()

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.

like image 376
iSasFTW Avatar asked Jan 05 '23 23:01

iSasFTW


1 Answers

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()
like image 124
Wayne Werner Avatar answered Jan 14 '23 14:01

Wayne Werner