zoukankan      html  css  js  c++  java
  • 【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

  • 相关阅读:
    select @@identity的用法
    类的实践
    UVA 1572 SelfAssembly(图论模型+拓扑排序)
    UVA 10562 Undraw the Trees(多叉树的dfs)
    sprintf与sscanf用法举例
    UVA 10129 Play on Words(欧拉回路)
    UVA 816 Abbott's Revenge(bfs)
    递增【二分】
    递增【二分】
    递增【二分】
  • 原文地址:https://www.cnblogs.com/heyang78/p/15315398.html
Copyright © 2011-2022 走看看