Python Forum
define a variable before looped websocket
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
define a variable before looped websocket
#1
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 assignment
Thanks ,
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  websocket help johnbob 0 894 Jan-13-2024, 06:32 AM
Last Post: johnbob
  trying to stream data from a websocket to client tomtom 2 2,047 Jan-12-2023, 03:01 PM
Last Post: Vadanane
  Help with Websocket-Client Module - Infinite Loop CluelessChris 0 3,928 Apr-25-2021, 01:53 PM
Last Post: CluelessChris
  Websocket conection closes abnormally Joshua_Omolo 2 3,895 Feb-17-2021, 08:03 AM
Last Post: Joshua_Omolo
  Websocket server not async? korenron 0 1,763 Sep-23-2019, 01:40 PM
Last Post: korenron
  need to understand better __setup__.py, websocket-client, packaging penright 0 2,956 Jul-08-2018, 09:40 PM
Last Post: penright
  Pro's and Con's of some different websocket libraries penright 0 2,357 Jun-29-2018, 12:07 PM
Last Post: penright

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020