2019独角兽企业重金招聘Python工程师标准>>>
import socketdef 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