[root@ToBeRoot alidata]# cat redis-cli #!/usr/bin/python #coding=utf8
import os import sys import time
class redisServer: def __init__(self,dbnum): self.db=[] self.dicta={} self.expires={} self.dbnum=dbnum for i in xrange(self.dbnum): self.db.append('redisDB'+str(i)) def redisDB(self,num): self.redisDB='redisDB'+str(num)
def setstring(self,key,value): self.dicta[key]=value
def setlist(self,key,*value): self.dicta[key]=list(value)
def getstringkey(self,key): print self.dicta[key]
def getlistkey(self,key,s,e): for i in self.dicta[key]: for j in i[s:e]: sys.stdout.write(str(j)+' ') print
if __name__=='__main__': databases=16 r = redisServer(databases)
a_str='Welcome to My Redis! '+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) print a_str while True: b_str=raw_input('127.0.0.1:6379 > ') b_list=b_str.split()
if b_list == ['config','get','databases']: print r.dbnum continue if b_list[0] == 'exit': break if b_list[0] == 'select': print b_list[1] continue if b_list[0] == 'set': # 字符串string name='booboo' r.setstring(b_list[1],b_list[2]) continue if b_list[0] == 'get': r.getstringkey(b_list[1]) continue if b_list[0] == 'rpush': # 列表list hot=[100,20,200,30] r.setlist(b_list[1],b_list[2:]) continue if b_list[0] == 'lrange': r.getlistkey(b_list[1],int(b_list[2]),int(b_list[3])) continue if b_list[0] not in ['dbsize','exit','select','set','get','rpush','lrange']: print "error" continue
[root@ToBeRoot alidata]# cp redis-cli /usr/local/bin/redis-cli-test [root@ToBeRoot alidata]# which redis-cli-test /usr/local/bin/redis-cli-test [root@ToBeRoot alidata]# redis-cli-test Welcome to My Redis! 2017-10-12 18:38:02 127.0.0.1:6379 > config get databases 16 127.0.0.1:6379 > select 0 0 127.0.0.1:6379 > set name booboo 127.0.0.1:6379 > get name booboo 127.0.0.1:6379 > set age 100 127.0.0.1:6379 > get age 100 127.0.0.1:6379 > rpush booboo 1 2 3 4 5 6 127.0.0.1:6379 > lrange booboo 0 4 1 2 3 4 5 127.0.0.1:6379 > exit; error 127.0.0.1:6379 > exit
|