Lawouach/WebSocket-for-Python — Issue #30: KeyboardInterrupt ignored in WebSocketClient
Author: adamflorin
Created: 2012-03-06 23:14:27+00:00
State: open

I'm using ws4py to build a simple command-line WS client. However, once it gets going, there's no way to bring it down without `kill`ing it. I see that `WebSocketClient` is using the `threading` package; how can I get it to close the connection on `KeyboardInterrupt` (or something similar)?

The code (set WEBSOCKET_SERVER_URL to a real WS server):

from ws4py.client.threadedclient import WebSocketClient

WEBSOCKET_SERVER_URL = "INSERT_VALID_URL_HERE"

class CantInterruptClient(WebSocketClient):
def opened(self):
print "Opened."

def closed(self, code):
print "Closed."

try:
print "Creating client..."
cant_interrupt_client = CantInterruptClient(WEBSOCKET_SERVER_URL)
cant_interrupt_client.connect()
except KeyboardInterrupt:
print "Attempting to quit..."
cant_interrupt_client.close()

Typical command line output:

$ python ws4py-interrupt.py
Creating client...
Opened.
^C^C^C^C^C^C^C^C