python redis 多機之間共享資料
阿新 • • 發佈:2018-11-04
本文轉自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很好用有木有!!超讚!