numeretti/utils/wsclient.py

41 lines
828 B
Python
Executable file

#!/usr/bin/python3 -u
import argparse
import ssl
import websocket
def get_parser():
p = argparse.ArgumentParser()
p.add_argument(
"-k",
"--no-ssl-verify",
action="store_false",
default=True,
dest="ssl_verify",
help="Disable SSL verify",
)
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)
run_opts = {}
if not args.ssl_verify:
run_opts = {"sslopt": {"cert_reqs": ssl.CERT_NONE}}
wsapp.run_forever(**run_opts)
if __name__ == "__main__":
main()