Python Forum

Full Version: define a variable before looped websocket
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 ,