define a variable before looped websocket - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: define a variable before looped websocket (/thread-21270.html) |
define a variable before looped websocket - korenron - Sep-22-2019 Hello , I have a wroking websocket and I wnat to only do something when I get new reaply / new data async def hello(websocket, path): #1 OldClientResponse="none" ClientResponse = await websocket.recv() print ("OLD is - " + OldClientResponse + "\r\nNew is " + ClientResponse) if (OldClientResponse != ClientResponse): print("New Client Response Is:\n\r" + ClientResponse) #going to do some code here OldClientResponse = ClientResponse #2 OldClientResponse="none" start_server = websockets.serve(hello, '10.0.0.79', 1234) asyncio.get_event_loop().run_until_complete(start_server) asyncio.get_event_loop().run_forever()The problem is that the program is allways enter the "if" , and its seem that old!=new all the time why? when #1 is on - I can understand why when #2 is on - I get error what am I missing ? when I do this async def hello(websocket, path): global OldClientResponse print (OldClientResponse) ClientResponse = await websocket.recv() print ("OLD is - " + OldClientResponse + "\r\nNew is " + ClientResponse) if (OldClientResponse != ClientResponse): print("New Client Response Is:\n\r" + ClientResponse) #going to do some code here OldClientResponse = ClientResponse OldClientResponse="none" start_server = websockets.serve(hello, '10.0.0.79', 1234) asyncio.get_event_loop().run_until_complete(start_server) asyncio.get_event_loop().run_forever()it print me the value "None" but doens't do the comapring saying : if (OldClientResponse != ClientResponse): UnboundLocalError: local variable 'OldClientResponse' referenced before assignmentThanks , |