一个可网络访问的命令行监控界面。
项目描述
监控服务器
监控服务器是一个提供命令行界面的服务器,用于请求各种信息。该服务器基于zc.ngi,因此我们可以使用zc.ngi测试基础设施来展示它。
>>> import zc.ngi.testing >>> import zc.monitor>>> connection = zc.ngi.testing.TextConnection() >>> server = zc.monitor.Server(connection)
服务器支持一组可扩展的命令。它通过命名zc.monitor.interfaces.IMonitorPlugin“实用程序”查找命令,这些实用程序由zope.component包定义。
为了展示这一点,我们将创建一个hello插件
>>> def hello(connection, name='world'): ... """Say hello ... ... Provide a name if you're not the world. ... """ ... connection.write("Hi %s, nice to meet ya!\n" % name)
并将其注册
>>> zc.monitor.register(hello)
当注册命令时,我们可以提供一个名称。为了展示这一点,我们将再次注册hello
>>> zc.monitor.register(hello, 'hi')
现在我们可以将hello命令提供给服务器
>>> connection.test_input('hi\n') Hi world, nice to meet ya! -> CLOSE
我们可以传递一个名称
>>> connection = zc.ngi.testing.TextConnection() >>> server = zc.monitor.Server(connection) >>> connection.test_input('hello Jim\n') Hi Jim, nice to meet ya! -> CLOSE
服务器附带了一些基本的命令。让我们注册它们,以便我们可以看到它们的作用。我们将使用简化的注册接口
>>> zc.monitor.register_basics()
首先是帮助命令。不输入帮助,会列出可用的命令
>>> connection = zc.ngi.testing.TextConnection() >>> server = zc.monitor.Server(connection) >>> connection.test_input('help\n') Supported commands: hello -- Say hello help -- Get help about server commands hi -- Say hello interactive -- Turn on monitor's interactive mode quit -- Quit the monitor -> CLOSE
我们可以通过指定命令名称来获取详细帮助
>>> connection = zc.ngi.testing.TextConnection() >>> server = zc.monitor.Server(connection) >>> connection.test_input('help help\n') Help for help: <BLANKLINE> Get help about server commands <BLANKLINE> By default, a list of commands and summaries is printed. Provide a command name to get detailed documentation for a command. <BLANKLINE> -> CLOSE>>> connection = zc.ngi.testing.TextConnection() >>> server = zc.monitor.Server(connection) >>> connection.test_input('help hello\n') Help for hello: <BLANKLINE> Say hello <BLANKLINE> Provide a name if you're not the world. <BLANKLINE> -> CLOSE
interactive命令将监控器切换到交互模式。如上所示,监控器通常响应单个命令然后关闭连接。在“交互模式”下,连接不会关闭,直到使用quit命令。当通过telnet进行诊断访问监控器时,这可能很有用。
>>> connection = zc.ngi.testing.TextConnection() >>> server = zc.monitor.Server(connection) >>> connection.test_input('interactive\n') Interactive mode on. Use "quit" To exit. >>> connection.test_input('help interactive\n') Help for interactive: <BLANKLINE> Turn on monitor's interactive mode <BLANKLINE> Normally, the monitor releases the connection after a single command. By entering the interactive mode, the monitor will not end the connection until you enter the "quit" command. <BLANKLINE> In interactive mode, an empty line repeats the last command. <BLANKLINE> >>> connection.test_input('help quit\n') Help for quit: <BLANKLINE> Quit the monitor <BLANKLINE> This is only really useful in interactive mode (see the "interactive" command). <BLANKLINE>
请注意,命令的结果并没有以“-> CLOSE”结束,这将表示连接已关闭。
请注意,交互模式允许你重复命令。
>>> connection.test_input('hello\n') Hi world, nice to meet ya! >>> connection.test_input('\n') Hi world, nice to meet ya! >>> connection.test_input('hello Jim\n') Hi Jim, nice to meet ya! >>> connection.test_input('\n') Hi Jim, nice to meet ya!
现在我们将使用quit来关闭连接。
>>> connection.test_input('quit\n') Goodbye. -> CLOSE
最后,值得注意的是,异常将在连接上生成跟踪输出。
>>> connection = zc.ngi.testing.TextConnection() >>> server = zc.monitor.Server(connection) >>> connection.test_input('hello Jim 42\n') # doctest: +ELLIPSIS Traceback (most recent call last): ... TypeError: hello() takes at most 2 arguments (3 given) <BLANKLINE> -> CLOSE
命令循环
使用“MORE”模式,命令可以发出信号,表示它们想要获取所有未来的用户输入。我们将实现一个愚蠢的示例来展示它是如何工作的。
这是一个实现计算器的命令。
>>> PROMPT = '.' >>> def calc(connection, *args): ... if args and args[0] == 'quit': ... return zc.monitor.QUIT_MARKER ... ... if args: ... connection.write(str(eval(''.join(args)))) ... connection.write('\n') ... ... connection.write(PROMPT) ... return zc.monitor.MORE_MARKER
如果我们注册这个命令…
>>> zc.monitor.register(calc)
…我们可以调用它,并获得提示。
>>> connection = zc.ngi.testing.TextConnection() >>> server = zc.monitor.Server(connection) >>> connection.test_input('calc\n') .
如果我们给它更多的输入,我们会得到结果和另一个提示。
>>> connection.test_input('2+2\n') 4 .>>> connection.test_input('4*2\n') 8 .
一旦我们完成,我们可以告诉计算器让我们离开。
>>> connection.test_input('quit\n') -> CLOSE
启动服务器
>>> import time >>> import zope.testing.loggingsupport, logging >>> loghandler = zope.testing.loggingsupport.InstalledHandler( ... None, level=logging.INFO)>>> zc.monitor.start(9644) ('', 9644)>>> print loghandler zc.ngi.async.server INFO listening on ('', 9644)>>> zc.monitor.last_listener.close() >>> zc.monitor.last_listener = None >>> time.sleep(0.1)>>> loghandler.clear()>>> zc.monitor.start(('127.0.0.1', 9644)) ('127.0.0.1', 9644)>>> print loghandler zc.ngi.async.server INFO listening on ('127.0.0.1', 9644)>>> zc.monitor.last_listener.close() >>> zc.monitor.last_listener = None >>> time.sleep(0.1)
绑定到端口0
>>> addr = zc.monitor.start(0) >>> addr == zc.monitor.last_listener.address True>>> zc.monitor.last_listener.close() >>> zc.monitor.last_listener = None >>> time.sleep(0.1)
尝试重新绑定到正在使用的端口
>>> loghandler.clear()>>> zc.monitor.start(('127.0.0.1', 9644)) ('127.0.0.1', 9644)>>> zc.monitor.start(('127.0.0.1', 9644)) False>>> print loghandler zc.ngi.async.server INFO listening on ('127.0.0.1', 9644) zc.ngi.async.server WARNING unable to listen on ('127.0.0.1', 9644) root WARNING unable to start zc.monitor server because the address ('127.0.0.1', 9644) is in use.>>> zc.monitor.last_listener.close() >>> zc.monitor.last_listener = None >>> time.sleep(0.1)>>> loghandler.uninstall()
变更历史
0.4.0.post1 (2019-12-06)
修复PyPI上的变更日志。
0.4.0 (2019-12-06)
使用新的Python 2.6/3.x兼容的异常语法。(这并不意味着该软件包已经兼容Python 3。)
0.3.1 (2012-04-27)
当将监控器绑定到Unix域套接字时,删除同一路径上存在的套接字,以便绑定成功。这可能会影响与zopectl调试行为相关的现有使用,但将更可预测。
0.3.0 (2011-12-12)
添加了简化的注册接口。
0.2.1 (2011-12-10)
为start添加了address选项,以便能够指定要绑定的适配器。
start现在返回正在监听的地址,这对于绑定到端口0很有用。
使用Python的doctest模块而不是已弃用的zope.testing.doctest。
0.2.0 (2009-10-28)
添加“MORE”模式,以便命令可以劫持用户交互
0.1.2 (2008-09-15)
错误修复:z3monitor服务器缺少handle_close方法,导致用户在给出命令之前关闭连接时记录错误。
0.1.1 (2008-09-14)
错误修复:修复并添加了对显示跟踪输出回归的测试。
0.1.0 (2008-09-14)
初始发布
项目详情
zc.monitor-0.4.0.post1.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 80412fd4b5045ca3b35566a867f19d56adacb8d306653dbf80e0dbeba7948389 |
|
MD5 | a50410d185bfd53ff2db3f5ea325ede7 |
|
BLAKE2b-256 | 0111f8a130f1cea0b22e8c9d2aaa10bbc519eba2bbce42656769e3de802bf922 |