import socket def format_message( args): """ Create redis message :param args: Message data """ l = u"*%d" % len(args) lines = [l.encode('utf-8')] for arg in args: if not isinstance(arg, (str,unicode)): arg = str(arg) if isinstance(arg, unicode): arg = arg.encode('utf-8') l = u"$%d" % len(arg) lines.append(l.encode('utf-8')) lines.append(arg) lines.append(b"") return b"\r\n".join(lines) def get( key): """ Get the value of a key :param key: Complexity ---------- O(1) """ args = ["GET"] args.append(key) return format_message(args) socCli = socket.socket() try: socCli.connect(('192.168.1.115', 6379)) except: print 'connect has failed' get_data=get("wdm") try: len=socCli.send(get_data) if not len: socCli.close() print 'connect has closed' except: print "send has failed" if len>0: try: get_data=socCli.recv(1024) if not get_data: socCli.close() print "read closed " except: print 'read failed' print get_data
一个redis的连接库的实现
最新推荐文章于 2023-12-23 08:00:00 发布