Redis in Python:遠端連線Redis伺服器
阿新 • • 發佈:2019-01-03
本文簡單介紹Python遠端連線redis-server的方法,其中redis-server執行在windows上。
步驟:
1.修改redis-server的配置
在執行redis-server的主機上執行命令ipconfig,找到ip地址,假設為192.168.1.100。
在redis.windows.conf 中找到bind 127.0.0.1這行並修改為bind 192.168.1.100。
redis.windows.conf 的內容摘錄如下:
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES # JUST COMMENT THE FOLLOWING LINE.# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ bind 192.168.1.100 # Protected mode is a layer of security protection, in order to avoid that # Redis instances left open on the internet are accessed and exploited. # # When protected mode is on and if:
2.執行redis-server
在命令視窗中切換到redis所在的目錄,執行命令:redis-server.exe redis.windows.conf 。
如果沒出錯的話redis-server會成功執行,出現如下圖的介面:
3.遠端連線
需要安裝redis-py:pip install redis
Python遠端連線程式碼如下:
>>> import redis >>> r = redis.Redis(host='192.168.1.100') >>> r.set('name', 'xiemanR') True >>> r.get('name') b'xiemanR' >>>host引數填上執行redis-server機器的ip,本文為192.168.1.100。