本文转自https://opensource.com/article/18/4/how-build-hello-redis-with-python,成果归原作者所有,我只是材料的搬运工
为方便以后的学习查找,记载下来,嘻嘻。。
首先保证python已安装redis
python -m pip install redis
redis共享数据步骤:
1. 导入redis库
2. 定义connection参数,包括server IP, redis端口号 ,redis passward
3. 示例化redis connection对象
4. 使用connection obj向redis server上传message
5. 使用connection obj从redis server拉取message
redis使用示例:
# step 1: import the redis-py client package
import redis
# step 2: define our connection information for Redis
# Replaces with your configuration information
redis_host = "localhost" #redis_host should be the server IP
redis_port = 6379
redis_password = ""
def hello_redis():
"""Example Hello Redis Program"""
# step 3: create the Redis Connection object
try:
# The decode_repsonses flag here directs the client to convert the responses from Redis into Python strings
# using the default encoding utf-8. This is client specific.
r = redis.StrictRedis(host=redis_host, port=redis_port, password=redis_password, decode_responses=True)
# step 4: Set the hello message in Redis
r.set("msg:hello", "Hello Redis!!!")
# step 5: Retrieve the hello message from Redis
msg = r.get("msg:hello")
print(msg)
except Exception as e:
print(e)
if __name__ == '__main__':
hello_redis()
python redis很好用有木有!!超赞!