I'm trying to use websockets in python, and i previously asked a question about this. I quickly realized, though, that the way i was connecting to the server was meant for "one off" messages, while what i want to do needs to listen for notifications constantly.
In the documentation for the python websocket client i can see the following code:
import websocket
import thread
import time
def on_message(ws, message):
print message
def on_error(ws, error):
print error
def on_close(ws):
print "### closed ###"
def on_open(ws):
def run(*args):
for i in range(3):
time.sleep(1)
ws.send("Hello %d" % i)
time.sleep(1)
ws.close()
print "thread terminating..."
thread.start_new_thread(run, ())
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://echo.websocket.org/",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
The thing is, i'm still quite new to python, and i don't completely understand this code. Since it's example code, and does things that i don't need it to, i would like to understand how it works. I, for example, don't understand what the for loop is needed for, or what the __name__ and the __main__ at the bottom are.
Is there a better way?
Thanks,
Sas :)
The for loop is probably just an example since it will just print Hello 0, Hello 1 and Hello 2.
__name__ == "__main__" is true when the Python interpreter is running a module as the main program, which you can read more about here. Whenever that happens, it assigns what functions should be used on message, error and when the socket closes. And when that's done it runs the WebSocket forever.
So, to create your own long-lived WebSocket you can copy this example code and change the on_message, on_error, on_close and on_open functions to do what you want them to do whenever these events occour. on_message activates whenever a message is sent, on_error when an error occours, on_close when the WebSocket closes and on_open when the WebSocket opens.
Related
I have an XPUB/XSUB device and a number of mock publishers running in one process. In a separate process, I want to connect a subscriber and print received message to the terminal. Below I will show two variants of a simple function to do just that. I have these functions wrapped as command-line utilities.
My problem is that the asyncio variant never receives messages.
On the other hand, the non-async variant works just fine. I have tested all cases for ipc and tcp transports. The publishing process never changes in my tests, except when I restart it to change transport. The messages are short strings and published roughly once per second, so we're not looking at performance problem.
The subscriber program sits indefinitely at the line msg = await sock.receive_multipart(). In the XPUB/XSUB device I have instrumentation that shows the forwarding of the sock.setsockopt(zmq.SUBSCRIBE, channel.encode()) message, same as when the non-async variant connects.
The asyncio variant (not working, as described)
def subs(url, channel):
import asyncio
import zmq
import zmq.asyncio
ctx = zmq.asyncio.Context.instance()
sock = ctx.socket(zmq.SUB)
sock.connect(url)
sock.setsockopt(zmq.SUBSCRIBE, channel.encode())
async def task():
while True:
msg = await sock.recv_multipart()
print(' | '.join(m.decode() for m in msg))
try:
asyncio.run(task())
finally:
sock.setsockopt(zmq.LINGER, 0)
sock.close()
The regular blocking variant (works fine)
def subs(url, channel):
import zmq
ctx = zmq.Context.instance()
sock = ctx.socket(zmq.SUB)
sock.connect(url)
sock.setsockopt(zmq.SUBSCRIBE, channel.encode())
def task():
while True:
msg = sock.recv_multipart()
print(' | '.join(m.decode() for m in msg))
try:
task()
finally:
sock.setsockopt(zmq.LINGER, 0)
sock.close()
For this particular tool there is no need to use asyncio. However, I am experiencing this problem elsewhere in my code too, where an asynchronous recv never receives. So I'm hoping that by clearing it up in this simple case I'll understand what's going wrong in general.
My versions are
import zmq
zmq.zmq_version() # '4.3.2'
zmq.__version__ # '19.0.2'
I'm on MacOS 10.13.6.
I'm fully out of ideas. Internet, please help!
A working async variant is
def subs(url, channel):
import asyncio
import zmq
import zmq.asyncio
ctx = zmq.asyncio.Context.instance()
async def task():
sock = ctx.socket(zmq.SUB)
sock.connect(url)
sock.setsockopt(zmq.SUBSCRIBE, channel.encode())
try:
while True:
msg = await sock.recv_multipart()
print(' | '.join(m.decode() for m in msg))
finally:
sock.setsockopt(zmq.LINGER, 0)
sock.close()
asyncio.run(task())
I conclude that, when using asyncio zmq, sockets must be created with a call running on the event loop from which the sockets will be awaited. Even though the original form did not do anything fancy with event loops, it appears that the socket has an event loop different from that used by asyncio.run. I'm not sure why, and I didn't open an issue with pyzmq because their docs show usage as in this answer, without comment.
Edit in response to a comment:
asyncio.run always creates a new event loop, so the loop presumably created for the sockets instantiated outside of the co-routine passed to asyncio.run (as in the asyncio variant in the original question) is obviously different.
I use websocket to make a long-live connection with the target wss-url successfully. But after receiving one message, the code caught an error named "rsv is not implemented, yet" and closed the connection.
It seems that few people have met this problem, which described as "rsv is not implemented, yet". And the API doc of websocket never mention this issue.
The main piece of my code:
def on_message(ws, message):
print(message)
def on_error(ws, error):
print("!!!find error!!!")
print(error)
def on_close(ws):
print("### why closed ???###")
websocket.enableTrace(True)
ws = websocket.WebSocketApp(url,
on_message = on_message,
on_error = on_error,
on_close = on_close,
header = header,
cookie = cookie,
)
ws.run_forever(origin = 'https://matters.news', skip_utf8_validation = True)
It will give me only one message, and then show that:
!!!find error!!!
rsv is not implemented, yet
send: b'\x88\x82\xd9\xe2\xcc\x8c\xda\n'
### why closed ???###
I received the same error and fixed it by removing:
'Sec-WebSocket-Extensions': 'permessage-deflate'
from my headers.
We have an ethernet RFID reader and a test program that allows us to communicate with the device trough websockets. The test program is probably written in C#. The python script i'm trying to write to communicate with the device however is not working.
the code looks like :
import websocket
import thread
import time
def on_message(ws, message):
print message
def on_error(ws, error):
print error
def on_close(ws):
print "### closed ###"
def test(ws):
ws.send("<command><heartbeat/></command>")
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://192.168.0.194:10001",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = test
ws.run_forever()
When i use a network sniffer i can clearly see multiple lines of back and forth communication between python and the reader but the reader is closing the connection.
The only feedback im getting is an error code with the following description:
Error when receiving the TCP channel
• Connection was aborted
• TCP/IP protocol error
• Permissible number of connections at one port was exceeded
• Check socket programming on the user side or find the fault with network analyzer
(Wireshark).
And with the network sniffer i can see all my messages have a bad checksum like:
Checksum: 0xcc83 (incorrect, should be 0xb8ad)
I'm trying to make use a websocket client in python 2.7. The client works fine when running with IDLE, as in the received messages are shown. However trying to insert the websocket messages in a tkinter text widget is not working. Messages are still being displayed in IDLE, but the tkinter window is not being displayed at all.
The package I'm using is called websocket-client 0.32.0 and I dowloaded it from here. I followed the instructions on how to make a javascript like api and somewhat adapted the code.
Here is my code:
from Tkinter import *
from websocket import *
master = Tk()
master.wm_title("Websocket Test")
minwidth = master.winfo_screenwidth()/4*3
minheight = master.winfo_screenheight()/4*3
master.minsize(width=minwidth, height=minheight)
master.resizable(0,0)
text = Text(master)
text.pack(expand=True,fill=BOTH)
def on_message(ws, message):
text.insert(END, message+"\n")
print "Received: "+message
return
def on_error(ws, error):
text.insert(END, error+"\n")
print error
return
def on_close(ws):
text.insert(END, "### closed ###\n")
print "### closed ###"
return
def on_open(ws):
ws.send("hi")
ws.send("test")
return
enableTrace(True)
ws = WebSocketApp("ws://echo.websocket.org/", on_message = on_message, on_error = on_error, on_close = on_close)
ws.on_open = on_open
ws.run_forever()
master.mainloop()
Here is what is shown in IDLE if that is also helpful:
--- request header ---
GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: echo.websocket.org
Origin: http://echo.websocket.org
Sec-WebSocket-Key: tXWAVVlaRoq2S4+p/z12gg==
Sec-WebSocket-Version: 13
-----------------------
--- response header ---
HTTP/1.1 101 Web Socket Protocol Handshake
Connection: Upgrade
Date: Fri, 21 Aug 2015 05:16:54 GMT
Sec-WebSocket-Accept: LH12LFLFaek6HgCnGIugF0sg9lA=
Server: Kaazing Gateway
Upgrade: websocket
-----------------------
send: '\x81\x82{b\x97\xfc\x13\x0b'
send: '\x81\x84\xa5J\xecf\xd1/\x9f\x12'
Received: hi
Received: test
I've been stuck on this for a while and I cannot find any solutions to this sort of problem. Any help is appreciated as I'm a newbie.
Well, I do not use websocket , but the issue seems to be that , when you do -
ws.run_forever()
The application goes into an infinite loop , so the control never reaches master.mainloop() , which is required for the tkinter to show up , and which is again another infinite loop, which is only exited when you close the gui.
What you really want is to run two infinite loop simultaneously, which would require you to run them in two different threads, currently both of them are defined on the same thread, so one would only run after the other has exited.
A quick fix would be to use master.after() with a certain delay and giving a function, and then in the function start a new thread for a function in which websocket connects to the server. so that both the infinite loops are in different threads.
But the best fix would be to use a button, lets call it 'Connect' . Then the callback for that button would be a function like on_connect() below , which starts the function to connect -
def connect_to_socket():
enableTrace(True)
ws = WebSocketApp("ws://echo.websocket.org/", on_message = on_message, on_error = on_error, on_close = on_close)
ws.on_open = on_open
ws.run_forever()
def on_connect():
import threading
t = threading.Thread(target=connect_to_socket)
t.start()
The button would be something like -
button = Button(master, text="Connect", command=on_connect)
Then you can place the button as you wish.
Thanks to Anand's response, I was able to figure out the problem. The working and updated code is posted below:
from Tkinter import *
from websocket import *
from threading import *
master = Tk()
master.wm_title("Team Black Client")
master.withdraw()
minwidth = master.winfo_screenwidth()/4*3
minheight = master.winfo_screenheight()/4*3
master.minsize(width=minwidth, height=minheight)
x = (master.winfo_screenwidth() - minwidth)/2
y = (master.winfo_screenheight() - minheight)/2
master.geometry("+"+str(x)+"+"+str(y))
master.deiconify()
master.resizable(0,0)
text = Text(master)
text.pack(expand=True,fill=BOTH)
def on_message(ws, message):
text.insert(END, message+"\n")
print "Received: "+message
return
def on_error(ws, error):
text.insert(END, error+"\n")
print error
return
def on_close(ws):
text.insert(END, "### closed ###\n")
print "### closed ###"
return
def on_open(ws):
ws.send("hi")
ws.send("test")
return
def connection():
enableTrace(True)
ws = WebSocketApp("ws://echo.websocket.org/", on_message = on_message, on_error = on_error, on_close = on_close)
ws.on_open = on_open
ws.run_forever()
return
t = Thread(target=connection)
t.start()
master.mainloop()
I'm trying to implement a REST client in python that reacts to messages received from the server received through an opened websocket with the concerned server.
Here is the scenario:
client opens a websocket with the server
from time to time, the server sends a message to the client
when the client receives the messages, it gets some information from
the server
The current client I have is able to open the websocket and to receive the message from the server. However, as soon as it receives the messages, it gets the information from the server then terminates while I'd like to keep it listening for other messages that will make it get a new content from the server.
Here is the piece of code I have:
def openWs(serverIp, serverPort):
##ws url setting
wsUrl = "ws://"+serverIp+":"+serverPort+"/websocket"
##open ws
ws = create_connection(wsUrl)
##send user id
print "Sending User ID..."
ws.send("user_1")
print "Sent"
##receiving data on ws
print "Receiving..."
result = ws.recv()
##getting new content
getUrl = "http://"+serverIp+":"+serverPort+"/"+result+"/entries"
getRest(getUrl)
I don't know if using threads is appropriate or not, I'm not expert in that.
If someone could help, it'll be great.
Thanks in advance.
I finished with this code, doing what I'm expecting. Git it from here
import websocket
import thread
import time
def on_message(ws, message):
print message
def on_error(ws, error):
print error
def on_close(ws):
print "### closed ###"
def on_open(ws):
def run(*args):
for i in range(3):
time.sleep(1)
ws.send("Hello %d" % i)
time.sleep(1)
ws.close()
print "thread terminating..."
thread.start_new_thread(run, ())
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://localhost:5000/chat",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()