diff --git a/examples/replica-connector-python/README.md b/examples/replica-connector-python/README.md new file mode 100644 index 0000000..2bf4089 --- /dev/null +++ b/examples/replica-connector-python/README.md @@ -0,0 +1,27 @@ +# Workload WebSocket Replica Connector Example with Python + +This example illustrates the process of establishing a WebSocket connection with a specific workload replica, sending a message, and receiving a response using Python within the Control Plane platform. + +This project has been validated with Python version `3.11.4`. + +## Prerequisites + +- Python installed. + +## Create a Virtual Python Environment (Optional) + +```bash +python -m venv venv +``` + +## Install Requirements + +```bash +pip install -r requirements.txt +``` + +## Run Script + +```bash +python main.py +``` diff --git a/examples/replica-connector-python/main.py b/examples/replica-connector-python/main.py new file mode 100644 index 0000000..d425387 --- /dev/null +++ b/examples/replica-connector-python/main.py @@ -0,0 +1,51 @@ +import websocket +import json + +## Variables ## +CONNECTION_URL = "wss:///remote" +REQUEST = { + "token": "", + "org": "", + "gvc": "", + "pod": "", + "container": "", + "command": ["echo", "hello", "world"], +} + + +## Functions ## +def on_message(ws, message): + print(f"Message from server: {message}") + + +def on_error(ws, error): + print(f"Error: {error}") + + +def on_close(ws, close_status_code, close_msg): + print(f"Connection closed, exit code: {close_status_code}") + + +def on_open(ws): + print("Connection opened") + + # Establish a connection with the replica + ws.send(json.dumps(REQUEST, indent=4)) + + +## START ## + +# Enable detailed logging to help with debugging +# websocket.enableTrace(True) + +# Create a WebSocketApp instance, specifying the server URL and the callback function +ws = websocket.WebSocketApp( + CONNECTION_URL, + on_open=on_open, + on_message=on_message, + on_error=on_error, + on_close=on_close, +) + +# Start the WebSocket connection and keep it open, processing incoming and outgoing messages +ws.run_forever() diff --git a/examples/replica-connector-python/requirements.txt b/examples/replica-connector-python/requirements.txt new file mode 100644 index 0000000..4126a48 --- /dev/null +++ b/examples/replica-connector-python/requirements.txt @@ -0,0 +1 @@ +websocket-client \ No newline at end of file