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

43 lines
868 B
Python
Raw Normal View History

2021-10-19 18:08:05 +00:00
import websocket
import _thread
import time
import ssl
2021-11-09 20:14:06 +00:00
import yaml
2021-10-19 18:08:05 +00:00
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, ())
2021-11-09 20:14:06 +00:00
def open_socket(url, port):
2021-10-19 18:08:05 +00:00
websocket.enableTrace(True)
2021-11-09 20:14:06 +00:00
ws = websocket.WebSocketApp(f"ws://{url}:{port}",
2021-10-19 18:08:05 +00:00
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})
2021-11-09 20:14:06 +00:00
if __name__ == "__main__":
with open('config.yaml', 'r') as f:
conf = yaml.safe_load(f)
open_socket(conf['ws']['url'], conf['ws']['port'])