【Python/Redis】书写Python程序访问Redis
本文用到的Python版本是Python 3.7.4,不保证在其它版本上也有同样效果。
安装完Python之后,就要安装redis模块,在cmd窗口中执行命令pip3 install redis,执行结果如下:
C:\hy\software>pip3 install redis Collecting redis Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x00000279BF69A548>, 'Connection to files.pythonhosted.org timed out. (connect timeout=15)')': /packages/a7/7c/24fb0511df653cf1a5d938d8f5d19802a88cef255706fdda242ff97e91b7/redis-3.5.3-py2.py3-none-any.whl Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x00000279BF684048>, 'Connection to files.pythonhosted.org timed out. (connect timeout=15)')': /packages/a7/7c/24fb0511df653cf1a5d938d8f5d19802a88cef255706fdda242ff97e91b7/redis-3.5.3-py2.py3-none-any.whl Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x00000279BF684908>, 'Connection to files.pythonhosted.org timed out. (connect timeout=15)')': /packages/a7/7c/24fb0511df653cf1a5d938d8f5d19802a88cef255706fdda242ff97e91b7/redis-3.5.3-py2.py3-none-any.whl Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x00000279BF684688>, 'Connection to files.pythonhosted.org timed out. (connect timeout=15)')': /packages/a7/7c/24fb0511df653cf1a5d938d8f5d19802a88cef255706fdda242ff97e91b7/redis-3.5.3-py2.py3-none-any.whl Downloading https://files.pythonhosted.org/packages/a7/7c/24fb0511df653cf1a5d938d8f5d19802a88cef255706fdda242ff97e91b7/redis-3.5.3-py2.py3-none-any.whl (72kB) 100% |████████████████████████████████| 81kB 746kB/s Installing collected packages: redis Successfully installed redis-3.5.3 You are using pip version 19.0.3, however version 21.2.4 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command. C:\hy\software>
这个安装好以后,我们就可以写程序连接到Redis了,请新建一个文本文件,名字不能是redis,扩展名为py,如1.py就很好,如果叫redis.py会出现奇怪的错误“module 'redis' has no attribute 'Redis'”这时也不要慌,把文件名改为别的就好了。
文件创建好后输入以下内容:
import redis re = redis.Redis(host='127.0.0.1', port=6379,db=0, password='ufo') re.set('dog','hashiqi') print(re.get('pig'))
这四句话分别是引包、连接、设值、取值,几乎不用注释。
然后打开cmd窗口,进入文件所在目录,执行python tryRedis.py
C:\hy\py>python tryRedis.py b'1234'
从输出可以看到,键pig的值1234取到了。
看看Redis控制台,dog的值也设置进去了:
C:\Redis-x64-3.2.100>redis-cli.exe -h 127.0.0.1 -p 6379 127.0.0.1:6379> auth ufo OK 127.0.0.1:6379> get dog "hashiqi" 127.0.0.1:6379>
以上步骤貌似不费劲,但也是我前进的一步,这一步是在以下网文的作者帮助下完成,在此向他们表示感谢:
1.https://www.runoob.com/w3cnote/python-redis-intro.html
2.https://www.cnblogs.com/igoodful/p/9747478.html
3.https://www.cnblogs.com/deliaries/p/11806164.html
END
分类:
Python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2017-09-20 【高中数学/排列组合】 从7个人选4个人负责元旦三天的值班工作,其中,第一天安排2人,第二和第三天均安排1人,且人员不重复,则共有几种安排方式?
2013-09-20 MySQL的IF函数
2013-09-20 遭遇java.lang.NoClassDefFoundError: org/apache/tomcat/PeriodicEventListener