Skip to content

Commit d778b9c

Browse files
committed
Updates for form, added login info
1 parent 5ab2df8 commit d778b9c

File tree

1 file changed

+59
-51
lines changed

1 file changed

+59
-51
lines changed

articles/azure-cache-for-redis/cache-python-get-started.md

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
22
title: 'Quickstart: Use Azure Cache for Redis in Python'
3-
description: In this quickstart, you learn how to create a Python App that uses Azure Cache for Redis.
3+
description: In this quickstart, you learn how to create a Python script that uses Azure Cache for Redis.
44
author: flang-msft
55

66
ms.author: franlanglois
7-
ms.date: 07/08/2024
7+
ms.date: 07/09/2024
88
ms.topic: quickstart
99
ms.service: cache
1010
ms.devlang: python
@@ -15,7 +15,7 @@ ms.custom: mvc, devx-track-python, mode-api, py-fresh-zinc
1515

1616
# Quickstart: Use Azure Cache for Redis in Python
1717

18-
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.
1919

2020
## Skip to the code on GitHub
2121

@@ -33,13 +33,17 @@ If you want to skip straight to the code, see the [Python quickstart](https://gi
3333

3434
## Install redis-py library
3535

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.
3737

3838
The following example used `pip3` for Python 3 to install `redis-py` on Windows 11 from an Administrator command prompt.
3939

4040
:::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.":::
4141

42-
## [Microsoft EntraID Authentication (recommended)](#tab/entraid)
42+
## Create a sample Python script to access your cache
43+
44+
Create a Python that uses either Microsoft Entra ID or access keys to connect to an Azure Cache for Redis. We recommend you use Microsoft Entra ID.
45+
46+
## [Microsoft Entra ID Authentication (recommended)](#tab/entraid)
4347

4448
[!INCLUDE [cache-entra-access](includes/cache-entra-access.md)]
4549

@@ -55,62 +59,66 @@ The following example used `pip3` for Python 3 to install `redis-py` on Windows
5559

5660
### Create a sample python app
5761

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`.
5963

6064
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`.
6165

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.
6367

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-
def hello_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+
def hello_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+
```
100104

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`
102108

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.":::
104112

105-
## Create a sample python app with reauthentication
113+
## Create a sample Python script with reauthentication
106114

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.
108116

109117
1. Create a new text file, add the following script, and save the file as `PythonApplication2.py`.
110118

111119
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`.
112120

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.
114122

115123
```python
116124
import time
@@ -179,9 +187,9 @@ Microsoft EntraID access tokens have limited lifespans, [averaging 75 minutes](/
179187

180188
1. Run `PythonApplication2.py` with Python. You should see results like the following example:
181189

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.":::
183191

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.
185193

186194
## [Access Key Authentication](#tab/accesskey)
187195

@@ -241,4 +249,4 @@ Run `PythonApplication1.py` with Python. You should see results like the followi
241249

242250
## Related content
243251

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

Comments
 (0)