Redis.exceptions.ConnectionError: Error -2 connection to localhost: 6379. Name or service unknown

I have this error when I run my code on the server, my env is debian, and Python2.7.3

Traceback (most recent call last):
  File "fetcher.py", line 4, in <module>
    import mirad.fetcher_tasks as tasks
  File "/home/mirad/backend/mirad/fetcher_tasks.py", line 75, in <module>
    redis_keys = r.keys('*')
  File "/home/mirad/backend/venv/local/lib/python2.7/site-packages/redis/client.py", line 863, in keys
    return self.execute_command('KEYS', pattern)
  File "/home/mirad/backend/venv/local/lib/python2.7/site-packages/redis/client.py", line 534, in execute_command
    connection.send_command(*args)
  File "/home/mirad/backend/venv/local/lib/python2.7/site-packages/redis/connection.py", line 532, in send_command
    self.send_packed_command(self.pack_command(*args))
  File "/home/mirad/backend/venv/local/lib/python2.7/site-packages/redis/connection.py", line 508, in send_packed_command
    self.connect()
  File "/home/mirad/backend/venv/local/lib/python2.7/site-packages/redis/connection.py", line 412, in connect
    raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error -2 connecting to localhost:6379. Name or service not known.

when I run redis-cliit works correctly without errors:

$ redis-cli 
127.0.0.1:6379> 
+4
source share
1 answer

It seems you are trying to connect redis to a server that is not identified by the current Debian environment. From Traceback, I see that you are trying to connect using the hostname as localhost,

r_server=redis.Redis(host="localhost",port=6379)

But your system is unable to understand "localhost", make an entry in the hosts file, since 127.0.0.1 is localhost. add below code to / etc / hosts

127.0.0.1 localhost

redis, ;

r_server=redis.Redis(host="localhost",port=6379) 
+7

All Articles