网络可访问的命令行监视接口。

zc.monitor的Python项目详细描述


监控服务器

监视器服务器是一个提供命令行接口的服务器 请求各种信息位。服务器是基于zc.ngi的,因此我们可以使用 ngi测试基础设施来演示它。

>>> import zc.ngi.testing
>>> import zc.monitor
>>> connection = zc.ngi.testing.TextConnection()
>>> server = zc.monitor.Server(connection)

服务器支持一组可扩展的命令。它向上看 命令名为zc.monitor.interfaces.imonitorplugin“utilities”,定义如下 通过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

命令循环

使用“更多”模式,命令可以发出信号,表示它们希望声明所有未来 用户输入。我们将实现一个愚蠢的示例来演示它是如何工作的。

这是一个实现计算器的命令。

>>> 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.3.1(2012-04-27)

  • 将监视器绑定到UNIX域套接字时,删除现有的 套接字位于同一路径,因此绑定成功。这可能会影响 关于ZopETCL调试行为的现有用法,但将 更容易预测。

0.3.0(2011-12-12)

  • 添加了一个简化的注册接口。

0.2.1(2011-12-10)

  • start添加了address选项,以便能够指定适配器 绑定到。
  • start现在返回正在监听的地址,这很有用 绑定到端口0时。
  • 使用python的doctestmodule而不是depreacted zope.testing.doctest

0.2.0(2009-10-28)

  • 添加“更多”模式,以便命令可以选择用户交互

0.1.2(2008-09-15)

  • 错误修复:Z3Monitor服务器缺少handle_close方法,该方法 导致在用户关闭连接之前记录错误 发出命令。

0.1.1(2008-09-14)

  • 错误修复:修复并添加了显示回溯时的回归测试。

0.1.0(2008-09-14)

初始版本

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Apache Flink外部Jar   创建和强制转换对象数组时发生java错误   Java,添加数组   具有相同包结构和类的java JAR   java Jenkins未能构建Maven项目   java为什么一个forloop比另一个更快,尽管它们做的“一样”?   servlets在将“/”站点迁移到Java EE包时处理contextpath引用   无法解析java MavReplugin:2.21或其某个依赖项   泛型如何编写比较器来泛化Java中的两种类型的对象?   java Android Emulator未在netbeans上加载   多线程Java使用线程对数组中的数字求和:在同步块中使用新变量作为锁:差异   java如何在JSP/servlet中设置<input>标记的值?