python:cmd+zeromq

#https://blog.51cto.com/capricorn/1891402
# coding=utf-8

from cmd import *
import sys
from subreqnode import SubReqNode
from multiprocessing import Queue, Process


def subscribe_callback(node):
    string = node.sub_socket.recv_string()
    print('{}'.format(string))


def start():
    node = SubReqNode("tcp://127.0.0.1:55566", "tcp://127.0.0.1:55567")
    node.subscribe('onu/online')
    node.start(subscribe_callback)


p = Process(target=start)
p.start()


class TestCmd(Cmd):
    def __init__(self):
        Cmd.__init__(self)
        Cmd.intro="test module"
    def do_test1(self,line):
        print "test test"
    def help_test1(self):
        print "test the module"
    def preloop(self):
        print u"enter test module"
    def postloop(self):
        print u"exit test module"
    def do_exit(self,line):
        return True
    def help_exit(self):
        print "exit command, return main loop"
    def do_quit(self,line):
        return True
    def help_quit(self):
        print "return main loop"


class MyShell(Cmd):
    def __init__(self):
        Cmd.__init__(self)
        self.prompt="Oxo>"
        self.intro="""
        test cmd package
        all exit
        """
        self.doc_header=",modify header "
        self.doc_leader='it is a leader'
        #node = SubReqNode("tcp://127.0.0.1:55566", "tcp://127.0.0.1:55567")
        #node.subscribe('onu/online')
        #self.node = node
        #self.process = Process(target=node.start, args=(subscribe_callback,))

    def preloop(self):
        print u"welcome info"

    def postloop(self):
        print u"post loop"

    #def precmd(self, line):
     #   print "print this line before do a command"
      #  return Cmd.precmd(self, line)

   # def postcmd(self, stop, line):
    #    print "print this line after do a command"
     #   return Cmd.postcmd(self, stop, line)
    def do_hello(self,line):
        print u"hello"
        print line
    def help_hello(self):
        print u"hello ~~~"

    def complete_hello(self,text,line,begidx,endidx):
        if not text:#
            completions=['timo','jack','jams']
            return completions
        else:
            completions=['timo','jack','jams']
            return [i for i in completions if i.startswith(text)]
    def do_test(self, line):
        i=TestCmd()
        i.prompt=self.prompt[:-1] +':Test>'
        i.cmdloop()
    def help_test(self):
        print u"it is a test"
    def do_exit(self,line):
        print u"exit!!"
        return True
    def help_exit(self):
        print "exit the ~~"
    def emptyline(self):
        pass
    def default(self,line):
        print u"no the command"


MyShell().cmdloop()

上一篇:D3基本图表绘制——折线图


下一篇:uniapp 微信发送订阅消息