Python + Redis: TypeError: an integer is required.

Today, while trying to write a simple python script to work with Redis (using the redis-py client), I had stumbled across a silly, yet time consuming error.

I had written simple RedisConnector class, which is working as a wrapper around the official redis-py client:

import redis
 
class RedisConnector(object):
 
   def __init__(self, host='localhost', port='6379'):
      super(RedisConnector, self).__init__()
      self._host = host
      self._port = port
      self.r = False
      self.connect()
 
   def connect(self):
      self.r = redis.StrictRedis(self.host, self.port, db=0)
      if self.r != False:
         return True
      else:
         return False
As it appears to be ok, there is one and very confusing error there, which will block you from doing any communication with redis. self._port needs to be an integer, not a string, otherwise you will be getting an error:

TypeError: an integer is required
The correct version of this snippet looks like this:

import redis
 
class RedisConnector(object):
 
   def __init__(self, host='localhost', port=6379):
      super(RedisConnector, self).__init__()
      self._host = host
      self._port = port
      self.r = False
      self.connect()
 
   def connect(self):
      self.r = redis.StrictRedis(self.host, self.port, db=0)
      if self.r != False:
         return True
      else:
         return False