Search code examples
pythonredisgetsetport

Python | Redis Connects but Stuck on Set or Get


Goal: Successfully set() and get() key-value pairs to local Redis via. Python.

I can connect to Redis. A possible issue is a firewall has closed port 6379. However, it is open.

Connection works with or without parameters: redis_db, redis_max_connections.

I suspect the issue is with Python, as I'm able to SET and GET via. Terminal:

127.0.0.1:6379> SET my-key TESTVAL
OK
127.0.0.1:6379> GET my-key
"TESTVAL"
127.0.0.1:6379> DEL my-key
(integer) 1
127.0.0.1:6379> GET my-key
(nil)

Code

import redis

redis_host = 'localhost'  # 127.0.0.1
redis_port = 6379
redis_password = ''  # 'your-redis-password'
redis_db = 2
redis_max_connections = 100

client = redis.Redis(host=redis_host, port=redis_port, password=redis_password, ssl=True, db=redis_db,
                           max_connections=redis_max_connections)
print('Connected!')

key = 'KEY'
value = 'VALUE'

client.set(key, value)
print('Data stored on Redis with key: ', key)

data = client.get(key)
print('Data retrieved from Redis with key: ', key)
print(data)

Runtime

(venv) me@laptop:~/GitHub/project$ python3 foo/bar/minimal_working_example.py
Connected!
|

Redis server is live:

(base) me@laptop:~$ redis-server
4965:C 17 Jan 2023 09:36:56.119 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4965:C 17 Jan 2023 09:36:56.119 # Redis version=7.0.7, bits=64, commit=00000000, modified=0, pid=4965, just started
4965:C 17 Jan 2023 09:36:56.119 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
4965:M 17 Jan 2023 09:36:56.119 * Increased maximum number of open files to 10032 (it was originally set to 1024).
4965:M 17 Jan 2023 09:36:56.119 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 7.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 4965
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

4965:M 17 Jan 2023 09:36:56.120 # Server initialized
4965:M 17 Jan 2023 09:36:56.120 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
4965:M 17 Jan 2023 09:36:56.120 * Ready to accept connections

Tested in a new Terminal:

(base) me@laptop:~$ redis-cli
127.0.0.1:6379> ping
PONG

Port 6379 is open:

(base) me@laptop:~$ nc -z localhost 6379
(base) me@laptop:~$ telnet localhost 6379
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

Solution

  • I wish my solution was grandiose...

    Simply not passing paramters sets up a Redis client of default paramters.

    client = redis.Redis()
    

    Which, if you do not specify localhost or port addresses, etc., Redis will use those default values in your local config file.