dist-iot-net/src/client/cli.py

43 lines
868 B
Python

import websocket
import _thread
import time
import ssl
import yaml
def on_message(ws, message):
print(f"<< {message}")
def on_error(ws, error):
print(error)
def on_close(ws, close_status_code, close_msg):
print(f"### closed. reason: {close_msg} ###")
def on_open(ws):
def run(*args):
running = True
while running:
outbound = input(">")
if outbound == 'q':
running = False
else:
ws.send(outbound)
ws.close()
_thread.start_new_thread(run, ())
def open_socket(url, port):
websocket.enableTrace(True)
ws = websocket.WebSocketApp(f"ws://{url}:{port}",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
if __name__ == "__main__":
with open('config.yaml', 'r') as f:
conf = yaml.safe_load(f)
open_socket(conf['ws']['url'], conf['ws']['port'])