@@ -6,92 +6,47 @@ ms.date: 05/16/2024
6
6
ms.author : samurp
7
7
---
8
8
9
- ### Create a Python Script
10
-
11
- If you disabled the "Requires Client Authorization" option when creating the Relay,
12
- you can send requests to the Hybrid Connections URL with any browser. For accessing
13
- protected endpoints, you need to create and pass a SAS Token, which is shown here.
14
-
15
- Here's a simple Python script that demonstrates sending requests to
16
- a Hybrid Connections URL with SAS Tokens utilizing WebSockets.
17
-
18
- ### Dependencies
19
-
20
- 1 . Install the following Python libraries using pip before running the client application
21
-
22
- `asyncio`, `json`, `logging`, `websockets`
23
-
24
- These libraries can be installed using the following command:
25
-
26
- ```bash
27
- pip install <package name>
28
- ```
29
- 2 . Generate a ` config.json ` file to store your connection details
30
-
31
- ``` json
32
- {
33
- "namespace" : " HYBRID_CONNECTION_NAMESPACE" ,
34
- "path" : " HYBRID_CONNECTION_ENTITY_NAME" ,
35
- "keyrule" : " SHARED_ACCESS_KEY_NAME" ,
36
- "key" : " SHARED_ACCESS_PRIMARY_KEY"
37
- }
38
- ```
39
- Replace the placeholders in brackets with the values you obtained when you created the hybrid connection.
40
-
41
- - `namespace` - The Relay namespace. Be sure to use the fully qualified namespace name; for example, `{namespace}.servicebus.windows.net`.
42
- - `path` - The name of the hybrid connection.
43
- - `keyrule` - Name of your Shared Access Policies key, which is `RootManageSharedAccessKey` by default.
44
- - `key` - The primary key of the namespace you saved earlier.
45
-
46
- 3 . Generate a helper function file for helper functions
47
-
48
- The following file is used as `relaylib.py` and have helper functions for WebSocket URL generation and SAS tokens
49
-
50
- [!INCLUDE [relay-python-helper-functions ](relay-python-helper-functions.md)]
51
-
52
9
### Write some code to send messages
53
10
54
11
1 . Ensure your dependency ` config.json ` and ` relaylib.py ` are available in your path
55
-
56
-
57
12
2 . Here's what your ` sender.py ` file should look like:
58
13
59
14
``` python
60
- import asyncio
61
- import json
62
- import logging
63
- import relaylib
64
- import websockets
65
-
66
- async def run_application(message, config):
67
- service_namespace = config["namespace"]
68
- entity_path = config["path"]
69
- sas_key_name = config["keyrule"]
70
- sas_key = config["key"]
71
- service_namespace += ".servicebus.windows.net"
72
-
73
- # Configure logging
74
- logging.basicConfig(level=logging.DEBUG) # Enable debug logging
75
-
76
- token = relaylib.createSasToken(service_namespace, entity_path, sas_key_name, sas_key)
77
- logging.debug("Token: %s", token)
78
- wss_uri = relaylib.createListenUrl (service_namespace, entity_path, token)
79
- logging.debug("WssURI: %s", wss_uri)
80
-
81
- try:
82
- async with websockets.connect(wss_uri) as websocket:
83
- logging.info("Sending message to Azure Relay WebSocket...")
84
- await websocket.send(json.dumps({"message": message}))
85
- logging.info("Message sent: %s", message)
86
- except Exception as e:
87
- logging.error("An error occurred: %s", str(e))
88
-
89
- if __name__ == "__main__":
90
- # Load configuration from JSON file
91
- with open("config.json") as config_file:
92
- config = json.load(config_file)
93
-
94
- asyncio.run(run_application("This is a message to Azure Relay Hybrid Connections!", config))
15
+ import asyncio
16
+ import json
17
+ import logging
18
+ import relaylib
19
+ import websockets
20
+
21
+ async def run_application (message , config ):
22
+ service_namespace = config[" namespace" ]
23
+ entity_path = config[" path" ]
24
+ sas_key_name = config[" keyrule" ]
25
+ sas_key = config[" key" ]
26
+ service_namespace += " .servicebus.windows.net"
27
+
28
+ # Configure logging
29
+ logging.basicConfig(level = logging.DEBUG ) # Enable debug logging
30
+
31
+ token = relaylib.createSasToken(service_namespace, entity_path, sas_key_name, sas_key)
32
+ logging.debug(" Token: %s " , token)
33
+ wss_uri = relaylib.createSendUrl (service_namespace, entity_path, token)
34
+ logging.debug(" WssURI: %s " , wss_uri)
35
+
36
+ try :
37
+ async with websockets.connect(wss_uri) as websocket:
38
+ logging.info(" Sending message to Azure Relay WebSocket..." )
39
+ await websocket.send(json.dumps({" message" : message}))
40
+ logging.info(" Message sent: %s " , message)
41
+ except Exception as e:
42
+ logging.error(" An error occurred: %s " , str (e))
43
+
44
+ if __name__ == " __main__" :
45
+ # Load configuration from JSON file
46
+ with open (" config.json" ) as config_file:
47
+ config = json.load(config_file)
48
+
49
+ asyncio.run(run_application(" This is a message to Azure Relay Hybrid Connections!" , config))
95
50
```
96
51
97
52
> [! NOTE ]
0 commit comments