Skip to content

Instantly share code, notes, and snippets.

@hbradio
Created October 4, 2018 20:57
Show Gist options
  • Save hbradio/de48feef6baef7d7d2f0667334297724 to your computer and use it in GitHub Desktop.
Save hbradio/de48feef6baef7d7d2f0667334297724 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import websocket
import thread
import time
import json
def on_message(ws, message):
print message
def on_error(ws, error):
print error
def on_close(ws):
print "### closed ###"
def on_open(ws):
def run(*args):
for i in range(30000):
time.sleep(1)
ws.send(json.dumps({"message": "Hello %d" % i}))
time.sleep(1)
ws.close()
print "thread terminating..."
thread.start_new_thread(run, ())
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://localhost:8000/ws/realtime/asdf/",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
@hbradio
Copy link
Author

hbradio commented Oct 4, 2018

pip install websocket-client

Run with Python 2.X

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment