numeretti/utils/wsclient.py

44 lines
881 B
Python
Raw Permalink Normal View History

2021-10-06 22:11:07 +02:00
#!/usr/bin/python3 -u
import argparse
2021-10-09 20:36:14 +02:00
import ssl
import time
2021-10-06 22:11:07 +02:00
import websocket
def get_parser():
p = argparse.ArgumentParser()
2021-10-09 20:36:14 +02:00
p.add_argument(
"-k",
"--no-ssl-verify",
action="store_false",
default=True,
dest="ssl_verify",
help="Disable SSL verify",
)
2021-10-06 22:11:07 +02:00
p.add_argument(
"--trace", action="store_true", default=False, help="Extra debugging"
)
p.add_argument("url")
return p
def on_message(wsapp, msg):
print(msg)
def main():
args = get_parser().parse_args()
websocket.enableTrace(args.trace)
wsapp = websocket.WebSocketApp(args.url, on_message=on_message)
2021-10-09 20:36:14 +02:00
run_opts = {}
if not args.ssl_verify:
run_opts = {"sslopt": {"cert_reqs": ssl.CERT_NONE}}
while True:
wsapp.run_forever(**run_opts)
time.sleep(1)
2021-10-06 22:11:07 +02:00
if __name__ == "__main__":
main()