blog新地址 Surprised => indulgence => Calm => Lost => Panic =>Calm

python 连接redis

Python admin 1487℃ 0评论

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

 

 

转载请注明:Imekaku-Blog » python 连接redis

喜欢 (0)or分享 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址