Skip to content

Commit f911d26

Browse files
authored
simplify listener python file
1 parent c8089ae commit f911d26

File tree

1 file changed

+24
-66
lines changed

1 file changed

+24
-66
lines changed

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

Lines changed: 24 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -54,49 +54,7 @@ a Hybrid Connections URL with SAS Tokens utilizing Websockets.
5454
1. Ensure your dependency `config.json` and `relaylib.py` are available in your path
5555

5656

57-
2. Import the dependencies into your `listener.py` script.
58-
59-
```python
60-
import asyncio
61-
import json
62-
import logging
63-
import relaylib
64-
import websockets
65-
```
66-
67-
3. Add the following code to the `listener.py` file. The main script should look like the following code:
68-
69-
```python
70-
async def run_application(config):
71-
serviceNamespace = config["namespace"]
72-
entityPath = config["path"]
73-
sasKeyName = config["keyrule"]
74-
sasKey = config["key"]
75-
serviceNamespace += ".servicebus.windows.net"
76-
# Configure logging
77-
logging.basicConfig(level=logging.INFO) # Enable DEBUG/INFO logging as appropriate
78-
79-
try:
80-
logging.debug("Generating SAS Token for: %s", serviceNamespace)
81-
token = relaylib.createSasToken(serviceNamespace, entityPath, sasKeyName, sasKey)
82-
logging.debug("Generating WebSocket URI")
83-
wssUri = relaylib.createListenUrl(serviceNamespace, entityPath, token)
84-
async with websockets.connect(wssUri) as websocket:
85-
logging.info("Listening for messages on Azure Relay WebSocket...")
86-
while True:
87-
message = await websocket.recv()
88-
logging.info("Received message: %s", message)
89-
except KeyboardInterrupt:
90-
logging.info("Exiting listener.")
91-
92-
if __name__ == "__main__":
93-
# Load configuration from JSON file
94-
with open("config.json") as config_file:
95-
config = json.load(config_file)
96-
97-
asyncio.run(run_application(config))
98-
```
99-
Here's what your `listener.py` file should look like:
57+
2. Here's what your `listener.py` file should look like:
10058

10159
```python
10260
import asyncio
@@ -106,31 +64,31 @@ a Hybrid Connections URL with SAS Tokens utilizing Websockets.
10664
import websockets
10765

10866
async def run_application(config):
109-
serviceNamespace = config["namespace"]
110-
entityPath = config["path"]
111-
sasKeyName = config["keyrule"]
112-
sasKey = config["key"]
113-
serviceNamespace += ".servicebus.windows.net"
114-
# Configure logging
115-
logging.basicConfig(level=logging.INFO) # Enable DEBUG/INFO logging as appropriate
116-
117-
try:
118-
logging.debug("Generating SAS Token for: %s", serviceNamespace)
119-
token = relaylib.createSasToken(serviceNamespace, entityPath, sasKeyName, sasKey)
120-
logging.debug("Generating WebSocket URI")
121-
wssUri = relaylib.createListenUrl(serviceNamespace, entityPath, token)
122-
async with websockets.connect(wssUri) as websocket:
123-
logging.info("Listening for messages on Azure Relay WebSocket...")
124-
while True:
125-
message = await websocket.recv()
126-
logging.info("Received message: %s", message)
127-
except KeyboardInterrupt:
67+
serviceNamespace = config["namespace"]
68+
entityPath = config["path"]
69+
sasKeyName = config["keyrule"]
70+
sasKey = config["key"]
71+
serviceNamespace += ".servicebus.windows.net"
72+
# Configure logging
73+
logging.basicConfig(level=logging.INFO) # Enable DEBUG/INFO logging as appropriate
74+
75+
try:
76+
logging.debug("Generating SAS Token for: %s", serviceNamespace)
77+
token = relaylib.createSasToken(serviceNamespace, entityPath, sasKeyName, sasKey)
78+
logging.debug("Generating WebSocket URI")
79+
wssUri = relaylib.createListenUrl(serviceNamespace, entityPath, token)
80+
async with websockets.connect(wssUri) as websocket:
81+
logging.info("Listening for messages on Azure Relay WebSocket...")
82+
while True:
83+
message = await websocket.recv()
84+
logging.info("Received message: %s", message)
85+
except KeyboardInterrupt:
12886
logging.info("Exiting listener.")
12987

13088
if __name__ == "__main__":
131-
# Load configuration from JSON file
132-
with open("config.json") as config_file:
133-
config = json.load(config_file)
89+
# Load configuration from JSON file
90+
with open("config.json") as config_file:
91+
config = json.load(config_file)
13492

135-
asyncio.run(run_application(config))
93+
asyncio.run(run_application(config))
13694
```

0 commit comments

Comments
 (0)