Created
October 4, 2018 20:57
-
-
Save hbradio/de48feef6baef7d7d2f0667334297724 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pip install websocket-client
Run with Python 2.X