zoukankan      html  css  js  c++  java
  • python和redis简单交互

    python和redis简单交互

    1.安装redis模块

    pip3 install redis
    

    2.redis模块简单使用:

    # /usr/bin/env python3
    import redis
    
    
    class Myredis(object):
        def __init__(self, host, port=6379):
            try:
                self.conn = redis.StrictRedis(host=host, port=port)
            except Exception as e:
                print(e)
            else:
                print('连接成功')
    
        def add(self, key, value):
            result = self.conn.set(key, value)
            print(result)
    
        def get(self, key):
            result = self.conn.get(key)
            print(result)
    
        def mod(self, key, value):
            result = self.conn.get(key)
            if result:
                result2 = self.conn.set(key, value)
                print(result2)
            else:
                print("key " + key + "is not found")
    
        def rem(self,key):
            result = self.conn.delete(key)
            if result != 0:
                print('删除成功')
    
    if __name__ == "__main__":
        myredis = Myredis('192.168.26.131')
        myredis.add('name','wanghui')
        myredis.mod('name','baby')
        myredis.get('name')
        myredis.rem('name')
    
    

  • 相关阅读:
    Git+GitHub+SaltStack
    系统运维
    Linux之Ubuntu
    TCP/IP 必知必会的十个问题
    Github常见操作和常见错误!
    Git钩子:自定义你的工作流
    Spring 梳理
    Spring 梳理
    Spring 梳理
    Spring boot 官网学习笔记
  • 原文地址:https://www.cnblogs.com/PrettyTom/p/6891191.html
Copyright © 2011-2022 走看看