41 lines
741 B
Python
41 lines
741 B
Python
|
#!/usr/bin/env python3
|
||
|
|
||
|
|
||
|
import sys
|
||
|
import json
|
||
|
import websocket
|
||
|
|
||
|
URL_PATH = ""
|
||
|
if len(sys.argv) > 1:
|
||
|
URL_PATH = sys.argv[1]
|
||
|
if URL_PATH.startswith('/'):
|
||
|
URL_PATH = URL_PATH[1::]
|
||
|
|
||
|
WEBSOCKET_URL = f"ws://example.com:5789/{URL_PATH}"
|
||
|
|
||
|
|
||
|
def on_open(web_socket):
|
||
|
request = {
|
||
|
"test": "example"
|
||
|
}
|
||
|
|
||
|
if len(sys.argv) == 4:
|
||
|
request = {
|
||
|
f"{sys.argv[2]}": f"{sys.argv[3]}"
|
||
|
}
|
||
|
print(f"Sent:\n\n{json.dumps(request)}")
|
||
|
web_socket.send(json.dumps(request))
|
||
|
|
||
|
|
||
|
def on_message(web_socket, message):
|
||
|
print(f"\nReceived:\n\n{message}")
|
||
|
|
||
|
|
||
|
websocket_client = websocket.WebSocketApp(
|
||
|
WEBSOCKET_URL,
|
||
|
on_open=on_open,
|
||
|
on_message=on_message
|
||
|
)
|
||
|
|
||
|
websocket_client.run_forever()
|