python 连接redis

4

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’>

 

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部