Skip to content

Instantly share code, notes, and snippets.

@lholmquist
Forked from jwendell/ws.py
Created May 18, 2017 20:20
Show Gist options
  • Save lholmquist/469c684c3e9dec76f2ea071b2c17c466 to your computer and use it in GitHub Desktop.
Save lholmquist/469c684c3e9dec76f2ea071b2c17c466 to your computer and use it in GitHub Desktop.
import websocket
try:
import thread
except ImportError: # TODO use Threading instead of _thread in python3
import _thread as thread
import time
import sys
import ssl
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(3):
# send the message, then wait
# so thread doesn't exit and socket
# isn't closed
# print("Sending: Hello %d" % i)
# ws.send("Hello %d" % i)
# time.sleep(3)
print('waiting %d seconds...' % 5)
time.sleep(5)
print('now closing...')
ws.close()
print("Thread terminating...")
thread.start_new_thread(run, ())
if __name__ == "__main__":
websocket.enableTrace(True)
host = sys.argv[1]
sslopt = {'cert_reqs':ssl.CERT_NONE}
headers = {'Authorization': 'Bearer ' + sys.argv[2]}
ws = websocket.WebSocketApp(host,
on_message=on_message,
on_error=on_error,
on_close=on_close,
header=headers)
ws.on_open = on_open
ws.run_forever(sslopt=sslopt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment