Skip to content

Commit c8089ae

Browse files
authored
simplify python client script
1 parent c4bc017 commit c8089ae

File tree

1 file changed

+1
-42
lines changed

1 file changed

+1
-42
lines changed

articles/azure-relay/includes/relay-hybrid-connections-websocket-requests-Python-get-started-client.md

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -53,49 +53,8 @@ a Hybrid Connections URL with SAS Tokens utilizing Websockets.
5353

5454
1. Ensure your dependency `config.json` and `relaylib.py` are available in your path
5555

56-
2. Import the dependencies into your `sender.py` script.
5756

58-
```python
59-
import asyncio
60-
import json
61-
import logging
62-
import relaylib
63-
import websockets
64-
```
65-
3. Add the following code to the `sender.py` file. The main script should look like the following code:
66-
67-
```python
68-
async def run_application(message, config):
69-
service_namespace = config["namespace"]
70-
entity_path = config["path"]
71-
sas_key_name = config["keyrule"]
72-
sas_key = config["key"]
73-
service_namespace += ".servicebus.windows.net"
74-
75-
# Configure logging
76-
logging.basicConfig(level=logging.DEBUG) # Enable debug logging
77-
78-
token = relaylib.createSasToken(service_namespace, entity_path, sas_key_name, sas_key)
79-
logging.debug("Token: %s", token)
80-
wss_uri = relaylib.createListenUrl(service_namespace, entity_path, token)
81-
logging.debug("WssURI: %s", wss_uri)
82-
83-
try:
84-
async with websockets.connect(wss_uri) as websocket:
85-
logging.info("Sending message to Azure Relay WebSocket...")
86-
await websocket.send(json.dumps({"message": message}))
87-
logging.info("Message sent: %s", message)
88-
except Exception as e:
89-
logging.error("An error occurred: %s", str(e))
90-
91-
if __name__ == "__main__":
92-
# Load configuration from JSON file
93-
with open("config.json") as config_file:
94-
config = json.load(config_file)
95-
96-
asyncio.run(run_application("This is a message to Azure Relay Hybrid Connections!", config))
97-
```
98-
Here's what your `sender.py` file should look like:
57+
2. Here's what your `sender.py` file should look like:
9958

10059
```python
10160
import asyncio

0 commit comments

Comments
 (0)