Trewq
Trewq

Reputation: 3237

python websocket client connected status - how to update so I get correct status?

I am able to connect to a node.js websocket server using python websocket. When I ask the python websocket client if it is connected to the server, it is not accurate.

Here is what happens:

Python 2.7.6 (default, Sep  9 2014, 15:04:36)
>>> import websocket
>>> ws = websocket.WebSocket()
>>> ws.connect("ws://localhost:8081") # my websocket server is running, so all good here.
>>> ws.connected
True      # This is correct. Now I kill the websocker server
>>> ws.connected
True      # this should be false since the server is dead.

The ws node.js module comes with wscat and you can run a websocket server by the command wscat -l 8081.

How do I get an accurate status that indicates the true connected/disconnected state?

Upvotes: 8

Views: 20116

Answers (1)

falsetru
falsetru

Reputation: 369124

The .connected attribute is not updated, unless close or shutdown, ... called, or recv* receive empty data.

Try to call recv(), then check the .connected attribute.

Upvotes: 14

Related Questions