websocket send command
-
I am experimenting with websocket send. I want to replace the hard coded command with variables. Unfortunately without success. See code:
import websocket import json from time import * #sending messages ------------------------------------ def send_data(CMD,DEV,CIRCUIT,VALUE): print("sending data : ",CMD,DEV,CIRCUIT,VALUE) x = ("%s" %('"cmd"' + ":" + '"' + CMD + '"' + "," + '"dev"' + ":" + '"' + DEV + '"' + "," + '"circuit"' + ":" + '"' + CIRCUIT + '"' + "," + '"value"' + ":" + '"' + VALUE + '"')) print(x) ws = websocket.WebSocket() ws.connect("ws://localhost/ws") #ws.send('{"cmd":"all"}') #ws.send('{"cmd":"set","dev":"relay","circuit":"3","value":"0"}') ws.send('{("%s" %('"cmd"' + ":" + '"' + CMD + '"' + "," + '"dev"' + ":" + '"' + DEV + '"' + "," + '"circuit"' + ":" + '"' + CIRCUIT + '"' + "," + '"value"' + ":" + '"' + VALUE + '"'))}') #ws.send('{"%s" %(x)}') ws.close() #main program ----------------------------------------- # send_data("set","relay","5","1") sleep(10)Any idea what I am doing wrong here?
Also command "cmd":"all" is not working.I am running evok on Stretch. Are there still problems with the new os?
I find it difficult to find the needed libraries for stretch and Python3. It would be helpfull to have some tested links for download.appriciate your help.
-
I found it:
#sending messages def ws_send(CMD, DEV, CIRCUIT, VALUE): ws = websocket.WebSocket() ws.connect(url) ws.send('{"cmd":"%s","dev":"%s","circuit":"%s","value":"%s"}' %(CMD,DEV,CIRCUIT,VALUE)) ws.close()
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login