How do I format a websocket request?

2024/10/10 16:16:57

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.

Answer

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()
https://en.xdnf.cn/q/69871.html

Related Q&A

cherrypy and wxpython

Im trying to make a cherrypy application with a wxpython ui. The problem is both libraries use closed loop event handlers. Is there a way for this to work? If I have the wx ui start cherrypy is that g…

What is the logic behind d3.js nice() ticks

I have generated some charts in d3.js. I use the following code to calculate the values to put in my y axis which works like a charm.var s = d3.scale.linear().domain([minValue, maxValue]); var ticks = …

Changing iterable variable during loop

Let it be an iterable element in python. In what cases is a change of it inside a loop over it reflected? Or more straightforward: When does something like this work?it = range(6) for i in it:it.remo…

Are CPython, IronPython, Jython scripts compatible with each other?

I am pretty sure that python scripts will work in all three, but I want to make sure. I have read here and there about editors that can write CPython, Jython, IronPython and I am hoping that I am look…

Python. Print mac address out of 6 byte string

I have mac address in 6 byte string. How would you print it in "human" readable format?Thanks

Cursors with postgres, where is the data stored and how many calls to the DB

Hi I am using psycopg2 for postgres access.I am trying to understand where "cursor" stores the returned rows. Does it store it in the database as a temporary table or is it on the clients en…

Django - How to allow only the owner of a new post to edit or delete the post?

I will be really grateful if anyone can help to resolve the issue below. I have the following Django project coding. The problem is: when the browser was given "/posts/remove/<post_id>/"…

Py4J has bigger overhead than Jython and JPype

After searching for an option to run Java code from Django application(python), I found out that Py4J is the best option for me. I tried Jython, JPype and Python subprocess and each of them have certai…

how to uninstall opencv-python package installed by using pip in anaconda?

I have tried to install OpenCV in anaconda. but when I use it, I figure out the instead of using OpenCV, the program using OpenCV-python and that why my program crashed. I type "conda uninstall op…

flask many to many join as done by prefetch_related from django

I have following Group and Contact model in flask with Sql Alchemy ORMgroup_contact = db.Table(group_contact,db.Column(group_id, db.Integer, db.ForeignKey(group.id)),db.Column(contact_id, db.Integer, d…