You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this Quickstart, you incorporate Azure Cache for Redis into a Python app to have access to a secure, dedicated cache that is accessible from any application within Azure.
18
+
In this Quickstart, you incorporate Azure Cache for Redis into a Python script to have access to a secure, dedicated cache that is accessible from any application within Azure.
19
19
20
20
## Skip to the code on GitHub
21
21
@@ -33,13 +33,17 @@ If you want to skip straight to the code, see the [Python quickstart](https://gi
33
33
34
34
## Install redis-py library
35
35
36
-
[Redis-py](https://pypi.org/project/redis/) is a Python interface to Azure Cache for Redis. Use the Python packages tool, `pip`, to install the `redis-py` package from a command prompt.
36
+
[Redis-py](https://pypi.org/project/redis/) is a Python interface to Azure Cache for Redis. Use the Python packages tool, `pip`, to install the `redis-py` package from a command prompt.
37
37
38
38
The following example used `pip3` for Python 3 to install `redis-py` on Windows 11 from an Administrator command prompt.
39
39
40
40
:::image type="content" source="media/cache-python-get-started/cache-python-install-redis-py.png" alt-text="Screenshot of a terminal showing an install of redis-py interface to Azure Cache for Redis.":::
@@ -55,62 +59,66 @@ The following example used `pip3` for Python 3 to install `redis-py` on Windows
55
59
56
60
### Create a sample python app
57
61
58
-
1. Create a new text file, add the following script, and save the file as `PythonApplication1.py`.
62
+
1. Create a new text file, add the following script, and save the file as `PythonApplication1.py`.
59
63
60
64
1. Replace `<Your Host Name>` with the value from your Azure Cache for Redis instance. Your host name is of the form `<DNS name>.redis.cache.windows.net`.
61
65
62
-
1. Replace `<Your Username>` with the values from your Microsoft EntraID user.
66
+
1. Replace `<Your Username>` with the values from your Microsoft Entra ID user.
63
67
64
-
```python
65
-
import redis
66
-
from azure.identity import DefaultAzureCredential
67
-
68
-
scope ="https://redis.azure.com/.default"
69
-
host ="<Your Host Name>"
70
-
port =6380
71
-
user_name ="<Your Username>"
72
-
73
-
74
-
defhello_world():
75
-
cred = DefaultAzureCredential()
76
-
token = cred.get_token(scope)
77
-
r = redis.Redis(host=host,
78
-
port=port,
79
-
ssl=True, # ssl connection is required.
80
-
username=user_name,
81
-
password=token.token,
82
-
decode_responses=True)
83
-
result = r.ping()
84
-
print("Ping returned : "+str(result))
85
-
86
-
result = r.set("Message", "Hello!, The cache is working with Python!")
87
-
print("SET Message returned : "+str(result))
88
-
89
-
result = r.get("Message")
90
-
print("GET Message returned : "+ result)
91
-
92
-
result = r.client_list()
93
-
print("CLIENT LIST returned : ")
94
-
for c in result:
95
-
print(f"id : {c['id']}, addr : {c['addr']}")
96
-
97
-
if__name__=='__main__':
98
-
hello_world()
99
-
```
68
+
```python
69
+
import redis
70
+
from azure.identity import DefaultAzureCredential
71
+
72
+
scope ="https://redis.azure.com/.default"
73
+
host ="<Your Host Name>"
74
+
port =6380
75
+
user_name ="<Your Username>"
76
+
77
+
78
+
defhello_world():
79
+
cred = DefaultAzureCredential()
80
+
token = cred.get_token(scope)
81
+
r = redis.Redis(host=host,
82
+
port=port,
83
+
ssl=True, # ssl connection is required.
84
+
username=user_name,
85
+
password=token.token,
86
+
decode_responses=True)
87
+
result = r.ping()
88
+
print("Ping returned : "+str(result))
89
+
90
+
result = r.set("Message", "Hello!, The cache is working with Python!")
91
+
print("SET Message returned : "+str(result))
92
+
93
+
result = r.get("Message")
94
+
print("GET Message returned : "+ result)
95
+
96
+
result = r.client_list()
97
+
print("CLIENT LIST returned : ")
98
+
for c in result:
99
+
print(f"id : {c['id']}, addr : {c['addr']}")
100
+
101
+
if__name__=='__main__':
102
+
hello_world()
103
+
```
100
104
101
-
Run `PythonApplication1.py` with Python. You should see results like the following example:
105
+
1. Before you run your Python code from a Terminal, make sure you authorize the terminal for using Microsoft Entra ID.
106
+
107
+
`azd auth login`
102
108
103
-
:::image type="content" source="media/cache-python-get-started/cache-python-completed.png" alt-text="Screenshot of a terminal showing a Python script to test cache access.":::
109
+
1. Run `PythonApplication1.py`with Python. You should see results like the following example:
110
+
111
+
:::image type="content" source="media/cache-python-get-started/cache-python-completed.png" alt-text="Screenshot of a terminal showing a Python script to test cache access.":::
104
112
105
-
## Create a sample python app with reauthentication
113
+
## Create a sample Python script with reauthentication
106
114
107
-
Microsoft EntraID access tokens have limited lifespans, [averaging 75 minutes](/entra/identity-platform/configurable-token-lifetimes#token-lifetime-policies-for-access-saml-and-id-tokens). In order to maintain a connection to your cache, you need to refresh the token. This example demonstrates how to do this using Python.
115
+
Microsoft Entra ID access tokens have limited lifespans, [averaging 75 minutes](/entra/identity-platform/configurable-token-lifetimes#token-lifetime-policies-for-access-saml-and-id-tokens). In order to maintain a connection to your cache, you need to refresh the token. This example demonstrates how to do this using Python.
108
116
109
117
1. Create a new text file, add the following script, and save the fileas`PythonApplication2.py`.
110
118
111
119
1. Replace `<Your Host Name>`with the value from your Azure Cache for Redis instance. Your host name is of the form `<DNS name>.redis.cache.windows.net`.
112
120
113
-
1. Replace `<Your Username>` with the values from your Microsoft EntraID user.
121
+
1. Replace `<Your Username>`with the values from your Microsoft Entra ID user.
114
122
115
123
```python
116
124
import time
@@ -179,9 +187,9 @@ Microsoft EntraID access tokens have limited lifespans, [averaging 75 minutes](/
179
187
180
188
1. Run `PythonApplication2.py`with Python. You should see results like the following example:
181
189
182
-
:::image type="content" source="media/cache-python-get-started/cache-python-completed.png" alt-text="Screenshot of a terminal showing a Python script to test cache access.":::
190
+
:::image type="content"source="media/cache-python-get-started/cache-python-completed.png" alt-text="Screenshot of a terminal showing a Python script to test cache access.":::
183
191
184
-
Unlike the first example, If your token expires, this example automatically refreshes it.
192
+
Unlike the first example, If your token expires, this example automatically refreshes it.
185
193
186
194
## [Access Key Authentication](#tab/accesskey)
187
195
@@ -241,4 +249,4 @@ Run `PythonApplication1.py` with Python. You should see results like the followi
241
249
242
250
## Related content
243
251
244
-
- [Create a simple ASP.NET web app that uses an Azure Cache for Redis.](./cache-web-app-howto.md)
252
+
- [Create a ASP.NET web app that uses an Azure Cache for Redis.](./cache-web-app-howto.md)
0 commit comments