python实现简单redis客户端(2)重构
来源:互联网 发布:淘宝网上图片无法显示 编辑:程序博客网 时间:2024/06/03 12:39
将客户端划分为通信模块和客户端模块进行重构
from socket import *class Connection: def __init__(self, host="localhost", port=6379): self.host = host self.port = port self.client=socket(AF_INET, SOCK_STREAM) def connect(self): self.client.connect((self.host,self.port)) def write(self,data): self.client.send(data.encode('utf8')) def read(self): data=self.client.recv(1024) return dataclass Client: def __init__(self, host="localhost", port=6379, password=None, db=None): self.connection = Connection(host=host, port=port) self.password = password self.db = db or 0 self.connection.connect() def format_command(self,*tokens, **kwargs): cmds = [] for t in tokens: cmds.append("$%s\r\n%s\r\n" % (len(t), t)) return "*%s\r\n%s" % (len(tokens), "".join(cmds)) def execute_command(self,cmd,*args,**kwargs): command = self.format_command(cmd,*args,**kwargs) self.connection.write(command) data = self.connection.read() return data def set(self,key,value): result = self.execute_command("SET",key,value) return result def get(self,key): value = self.execute_command("GET",key) return valueif __name__ == '__main__': client=Client() client.set('dog','love') value = client.get('dog') print(value)
效果:
阅读全文
0 0
- python实现简单redis客户端(2)重构
- python实现简单redis客户端(1)
- python实现简单redis客户端(3)使用asyncio添加异步支持
- python实现redis客户端单例+hbase客户端单例
- 简单实现python socket 服务器客户端
- Python 用Redis简单实现分布式爬虫
- 简单使用redis客户端
- Redis客户端简单封装
- Redis客户端简单比较
- redis☞ python客户端
- python--xmlrpc调用简单实现&c++和python实现客户端
- 最简单的Redis客户端实现——Golang
- python的redis客户端性能
- python 上安装redis客户端
- Redis的Python客户端redis-py
- Redis的Python客户端redis-py
- Redis的Python客户端redis-py
- Redis的Python客户端redis-py
- SpringMVC学习(四)-处理模型数据
- 改进记录
- JAVA窗口工具类代码拆解说明
- 阿里秋招面试
- Mybatis的学习之路(二)
- python实现简单redis客户端(2)重构
- 算法提高 ADV-113 c++_ch02_0
- 链接03
- 算法提高 ADV-114 c++_ch02_03
- Ubuntu16.04+CUDA8.0+Caffe配置
- 算法提高 ADV-115 c++_ch03_02
- css与内联样式
- 【二叉树】107. Binary Tree Level Order Traversal II
- 算法提高 ADV-117 进制转换