Skip to content

Add replica-connector-python example #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions examples/replica-connector-python/README.md
Original file line number Diff line number Diff line change
@@ -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
```
51 changes: 51 additions & 0 deletions examples/replica-connector-python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import websocket
import json

## Variables ##
CONNECTION_URL = "wss://<deployment.status.remote>/remote"
REQUEST = {
"token": "<token>",
"org": "<org-name>",
"gvc": "<gvc-name>",
"pod": "<replica-name>",
"container": "<container-name>",
"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()
1 change: 1 addition & 0 deletions examples/replica-connector-python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
websocket-client
Loading