本文简单介绍Python远程连接redis-server的方法,其中redis-server运行在windows上。
在运行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:
在命令窗口中切换到redis所在的目录,执行命令:redis-server.exe redis.windows.conf 。
如果没出错的话redis-server会成功运行,出现如下图的界面:
pip install redis
>>> 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。