六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 53|回复: 0

这是我自己使用memcached工具

[复制链接]

升级  4%

62

主题

62

主题

62

主题

举人

Rank: 3Rank: 3

积分
212
 楼主| 发表于 2013-1-15 02:57:37 | 显示全部楼层 |阅读模式
是python 写的,只适用到最基本的类库,应该各个版本都能够运行。是一个命令提示符界面。启动以后会有简单提示。
 
#! /usr/bin/env python# -*- coding: iso-8859-1 -*-import telnetlibimport timeimport stringimport sysimport osimport os.pathENCODING = sys.getfilesystemencoding()host = {}host['ip']=''host['port']=11211tn = Nonedef sendCmd(tn,cmd):  tn.write( cmd + "\n")  time.sleep(0.5)  print "\t --" + cmd    #print tn.read_some()    #print "\t--read_very_eager"  #print tn.read_very_eager()   print tn.read_very_lazy()   print tn.read_very_eager() def test():    global tn  tn = telnetlib.Telnet(host['ip'],host['port'])  #tn.set_debuglevel(2)  #print tn.read_some( )     #sendCmd(tn,"stats")  #sendCmd(tn,"stats sizes")  #sendCmd(tn,"stats items")  #sendCmd(tn,"stats slabs")  #sendCmd(tn,"stats malloc")  #sendCmd(tn,"stats maps")  #sendCmd(tn,"stats cachedump 1 10000")    #sendCmd(tn,"stats detail dump")  #stats  [on|off|dump]      tn.close()  class Shell:    shortCmdMap = {}    def statsCmd(self,uCmd):    global tn    if tn == None:      outPut(u" memcached is not connect")      return     cmd = uCmd.encode(ENCODING)    sendCmd(tn,cmd)        def open(self,uCmd):    global tn    parts = string.split(uCmd," ")    if len(parts) == 3 :      host['ip'] = parts[1]      host['port'] = parts[2]      tn = telnetlib.Telnet(parts[1],int(parts[2]))    else:      outPut(u" open connent param error")                            def handleCmd(self,uCmd):    global tn,host    if tn == None:      outPut(u" memcached is not connect")    else:      outPut(u"connect to:" + host['ip'] + u"," + host['port'])    if uCmd == u"env":      print os.environ      return    if uCmd == u"list" or uCmd == u"l":      files = os.listdir("")      self.shortCmdMap = {}      index = 0      for f in files:                uFile = f.decode(ENCODING)        #outPut(uFile)        uFile = uFile.lower()        if uFile.endswith(".bat"):          index += 1          uShortCmd = u"#" + str(index)          self.shortCmdMap[uShortCmd] = uFile          outPut(uShortCmd +" " +uFile)      return    if uCmd == u"help" or uCmd == u"h":      printHelpInfo()      return    if uCmd.startswith(u"#"):      uRealCmd = self.shortCmdMap[uCmd]      if uRealCmd != None:        os.system(uRealCmd)        return    print uCmd    if uCmd.startswith(u"open "):      self.open(uCmd)      return     if uCmd.startswith(u"stats"):      self.statsCmd(uCmd)      return         def processCmd(self):    while True:      cmd = raw_input(u'\n$:'.encode(ENCODING))      uCmd = cmd.decode(ENCODING)      uCmd = string.strip(uCmd)      if uCmd == u"exit":        outPut("bye-bye")        break       else:              self.handleCmd(uCmd)      outPut(uCmd)    def printHelpInfo():  outPut(u"help(h) print help ")    outPut(u"list(l) list all command")  outPut(u"env display all env info")  outPut(u"open host port")  outPut(u"stats")    outPut(u"stats reset")  outPut(u"stats sizes")  outPut(u"stats items")  outPut(u"stats slabs")  outPut(u"stats malloc")  outPut(u"stats maps")  outPut(u"stats maps")  outPut(u"stats cachedump slab_id limit_num")  outPut(u"exit")    def outPut(ustr):  print ustr.encode(ENCODING)  if __name__ == '__main__':    outPut(u"memcached stat shell:")  s = Shell()  printHelpInfo()  s.processCmd()
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表