Last active
August 9, 2023 10:11
-
-
Save sphinxid/5c46c82f3e7a33743f255badf0632a89 to your computer and use it in GitHub Desktop.
python 3 script to test connection to a redis and do simple GET/SET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import redis | |
def test_redis(host, port, password=None): | |
# Connect to Redis | |
client = redis.StrictRedis(host=host, port=port, password=password, decode_responses=True) | |
# Set a key-value pair | |
client.set("test_key", "Hello Redis!") | |
# Get the value of the key | |
value = client.get("test_key") | |
print(f"Value of 'test_key': {value}") | |
if __name__ == "__main__": | |
# Configuration | |
REDIS_HOST = "localhost" | |
REDIS_PORT = 6379 | |
REDIS_PASSWORD = "redispassword" | |
# If password input is an empty string, set it to None | |
if not REDIS_PASSWORD.strip(): | |
REDIS_PASSWORD = None | |
# Test Redis | |
test_redis(REDIS_HOST, REDIS_PORT, REDIS_PASSWORD) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ensure you have the redis Python library installed:
pip install redis