Python连接redis
代码
#!/usr/bin/env python # coding: utf-8 import redis REDIS_IP = "127.0.0.1" REDIS_PORT = 6379 def get_value_by_key(Key): try: redisHandler = redis.StrictRedis(host=REDIS_IP, port=REDIS_PORT) if not Key: print "The key is none" raise AssertionError() responese = redisHandler.get(Key) print type(responese) if responese == "": print "The value is none" raise AssertionError() else: print "The value = " + responese except: print "Error" if __name__ == "__main__": Key = "name" get_value_by_key(Key)
如果能够从redis中取出key对应的value,那么responese的类型是<type ‘str’>
如果没有取出,那么就会报错,类型是<type ‘NoneType’>