Skip to content

Instantly share code, notes, and snippets.

@sphinxid
Last active August 9, 2023 10:11
Show Gist options
  • Save sphinxid/5c46c82f3e7a33743f255badf0632a89 to your computer and use it in GitHub Desktop.
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
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)
@sphinxid
Copy link
Author

sphinxid commented Aug 9, 2023

Ensure you have the redis Python library installed:

pip install redis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment