扭曲的沙井:如何访问应用程序中的服务器?

发布于 2024-12-01 06:40:26 字数 4947 浏览 0 评论 0原文

我需要在运行时连接到我的 twisted 应用程序,并且我正在尝试获取 twisted.manhole 为我工作以实现这一目标。我使用的是 Mac OSX 10.6,默认安装了twisted 8.2。

使用twistd的示例服务器可以工作。启动时有关于 md5、sha 和twisted.protocols.telnet 的 DeprecationWarnings,但沙井服务器实际上做了它应该做的事情,我可以访问我的应用程序的内部:

host:client user$ telnet localhost 4040
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

twisted.manhole.telnet.ShellFactory
Twisted 8.2.0
username: admin
password: *****
>>> dir()
['_', '__builtins__', 'factory', 'service']
>>> factory
<twisted.manhole.telnet.ShellFactory instance at 0x101256440>
>>> service
<twisted.application.internet.TCPServer instance at 0x10124ff38>
>>> service.parent
<twisted.application.service.MultiService instance at 0x1014b0cf8>
>>> 

现在我尝试将其集成到我的应用程序中:

# test_manhole.tac

from twisted.application.internet import TCPServer
from twisted.application.service  import Application, IServiceCollection
from twisted.manhole.telnet       import ShellFactory

shell_factory = ShellFactory()
shell_factory.username = 'admin'
shell_factory.password = 'admin'
shell_factory.namespace['some_value'] = 42
shell_tcp_server = TCPServer(4040, shell_factory)

application = Application('test')
serviceCollection = IServiceCollection(application)

shell_tcp_server.setServiceParent(serviceCollection)

运行上面的代码在 shell 中:

host:server user$ twistd -noy test_manhole.tac
(omitting the same DeprecationWarnings about md5, sha and twisted.protocols.telnet as earlier)
2011-08-24 16:52:13+1000 [-] Log opened.
2011-08-24 16:52:13+1000 [-] twistd 8.2.0 (/usr/bin/python2.6 2.6.1) starting up.
2011-08-24 16:52:13+1000 [-] reactor class: twisted.internet.selectreactor.SelectReactor.
2011-08-24 16:52:13+1000 [-] twisted.manhole.telnet.ShellFactory starting on 4040
2011-08-24 16:52:13+1000 [-] Starting factory <twisted.manhole.telnet.ShellFactory instance at 0x1012cfdd0>
2011-08-24 16:52:13+1000 [-] start service:  <twisted.application.internet.TCPServer instance at 0x1012cff80>

在第二个 shell 中,运行 telnet 客户端:

host:client user$ telnet localhost 4040
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

twisted.manhole.telnet.ShellFactory
Twisted 8.2.0
username: admin
password: *****
>>> dir()
['_', '__builtins__', 'factory', 'service', 'some_value']
>>> factory
<twisted.manhole.telnet.ShellFactory instance at 0x1012cfdd0>
>>> service
>>> service == None
True
>>> service.parent
      ...
exceptions.AttributeError: 'NoneType' object has no attribute 'parent'
>>> some_value
42

因此,服务对象似乎无法用于访问应用程序内部。

好的,由于 twisted.protocols.telnet 无论如何似乎已被 twisted.conch.telnet 取代,请尝试使用较新的模块:

# test_manhole_2.tac

from twisted.application.service import Application, IServiceCollection
from twisted.conch.manhole_tap   import makeService

options = \
{
    # for some reason, these must
    # all exist, even if None
    'namespace'  : None,
    'passwd'     : 'users.txt',
    'sshPort'    : None,
    'telnetPort' : '4040',
}

shell_service = makeService(options)

application = Application('test')
serviceCollection = IServiceCollection(application)

shell_service.setServiceParent(serviceCollection)

“密码文件”users .txt 可能只包含一行示例用户名和密码,例如 admin:admin

启动测试服务器:

host:server user$ twistd -noy test_manhole_2.tac
(omitting DeprecationWarnings about md5 and sha)
2011-08-24 17:44:26+1000 [-] Log opened.
2011-08-24 17:44:26+1000 [-] twistd 8.2.0 (/usr/bin/python2.6 2.6.1) starting up.
2011-08-24 17:44:26+1000 [-] reactor class: twisted.internet.selectreactor.SelectReactor.
2011-08-24 17:44:26+1000 [-] twisted.internet.protocol.ServerFactory starting on 4040
2011-08-24 17:44:26+1000 [-] Starting factory <twisted.internet.protocol.ServerFactory instance at 0x10181e710>
2011-08-24 17:44:26+1000 [-] start service:  <twisted.application.service.MultiService instance at 0x1012553b0>
2011-08-24 17:44:26+1000 [-] start service:  <twisted.application.internet.TCPServer instance at 0x10181e998>

在第二个 shell 中,运行 telnet 客户端 - 请注意,其中一些实际上是猜测,因为有问题的 Mac OSX readline(或其他任何责任)似乎吞噬了一些 shell 输出:

host:client user$ telnet localhost 4040
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Username: admin
Password: *****

>>> dir()
['__builtins__']

所以现在看来​​我已经从一个无用的 service 对象变成了根本没有对象。

如何正确使用twisted.manhole?

I need to connect to my twisted application at runtime and I am trying to get twisted.manhole to work for me to that end. I am on Mac OSX 10.6 with twisted 8.2 as installed by default.

The sample server using twistd works. There are DeprecationWarnings at startup about md5, sha and twisted.protocols.telnet, but the manhole server actually does what it is supposed to and I can access internals of my application:

host:client user$ telnet localhost 4040
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

twisted.manhole.telnet.ShellFactory
Twisted 8.2.0
username: admin
password: *****
>>> dir()
['_', '__builtins__', 'factory', 'service']
>>> factory
<twisted.manhole.telnet.ShellFactory instance at 0x101256440>
>>> service
<twisted.application.internet.TCPServer instance at 0x10124ff38>
>>> service.parent
<twisted.application.service.MultiService instance at 0x1014b0cf8>
>>> 

Now I try to integrate this into my application:

# test_manhole.tac

from twisted.application.internet import TCPServer
from twisted.application.service  import Application, IServiceCollection
from twisted.manhole.telnet       import ShellFactory

shell_factory = ShellFactory()
shell_factory.username = 'admin'
shell_factory.password = 'admin'
shell_factory.namespace['some_value'] = 42
shell_tcp_server = TCPServer(4040, shell_factory)

application = Application('test')
serviceCollection = IServiceCollection(application)

shell_tcp_server.setServiceParent(serviceCollection)

Run the above code in a shell:

host:server user$ twistd -noy test_manhole.tac
(omitting the same DeprecationWarnings about md5, sha and twisted.protocols.telnet as earlier)
2011-08-24 16:52:13+1000 [-] Log opened.
2011-08-24 16:52:13+1000 [-] twistd 8.2.0 (/usr/bin/python2.6 2.6.1) starting up.
2011-08-24 16:52:13+1000 [-] reactor class: twisted.internet.selectreactor.SelectReactor.
2011-08-24 16:52:13+1000 [-] twisted.manhole.telnet.ShellFactory starting on 4040
2011-08-24 16:52:13+1000 [-] Starting factory <twisted.manhole.telnet.ShellFactory instance at 0x1012cfdd0>
2011-08-24 16:52:13+1000 [-] start service:  <twisted.application.internet.TCPServer instance at 0x1012cff80>

In a second shell, run a telnet client:

host:client user$ telnet localhost 4040
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

twisted.manhole.telnet.ShellFactory
Twisted 8.2.0
username: admin
password: *****
>>> dir()
['_', '__builtins__', 'factory', 'service', 'some_value']
>>> factory
<twisted.manhole.telnet.ShellFactory instance at 0x1012cfdd0>
>>> service
>>> service == None
True
>>> service.parent
      ...
exceptions.AttributeError: 'NoneType' object has no attribute 'parent'
>>> some_value
42

So it seems the service object is not usable to access application internals.

OK, since twisted.protocols.telnet anyway seems to have been superseded by twisted.conch.telnet, try using the newer module:

# test_manhole_2.tac

from twisted.application.service import Application, IServiceCollection
from twisted.conch.manhole_tap   import makeService

options = \
{
    # for some reason, these must
    # all exist, even if None
    'namespace'  : None,
    'passwd'     : 'users.txt',
    'sshPort'    : None,
    'telnetPort' : '4040',
}

shell_service = makeService(options)

application = Application('test')
serviceCollection = IServiceCollection(application)

shell_service.setServiceParent(serviceCollection)

The 'password file' users.txt may contain as little as one line with a sample username and password, e.g. admin:admin.

Start the test server:

host:server user$ twistd -noy test_manhole_2.tac
(omitting DeprecationWarnings about md5 and sha)
2011-08-24 17:44:26+1000 [-] Log opened.
2011-08-24 17:44:26+1000 [-] twistd 8.2.0 (/usr/bin/python2.6 2.6.1) starting up.
2011-08-24 17:44:26+1000 [-] reactor class: twisted.internet.selectreactor.SelectReactor.
2011-08-24 17:44:26+1000 [-] twisted.internet.protocol.ServerFactory starting on 4040
2011-08-24 17:44:26+1000 [-] Starting factory <twisted.internet.protocol.ServerFactory instance at 0x10181e710>
2011-08-24 17:44:26+1000 [-] start service:  <twisted.application.service.MultiService instance at 0x1012553b0>
2011-08-24 17:44:26+1000 [-] start service:  <twisted.application.internet.TCPServer instance at 0x10181e998>

In a second shell, run a telnet client - note that some of this is actually guesswork as the buggy Mac OSX readline (or whatever else is the to blame) seems to swallow some shell output:

host:client user$ telnet localhost 4040
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Username: admin
Password: *****

>>> dir()
['__builtins__']

So now it seems I've gone from having a useless service object to no object at all.

How is twisted.manhole used correctly ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御弟哥哥 2024-12-08 06:40:26

继续使用twisted.conch并将一些内容放入命名空间字典中。

namespace = {"your_application_object": some_object}

options = {
    # for some reason, these must
    # all exist, even if None
    'namespace'  : namespace,
    'passwd'     : 'users.txt',
    'sshPort'    : None,
    'telnetPort' : '4040',
}

Keep using twisted.conch and put something in the namespace dictionary.

namespace = {"your_application_object": some_object}

options = {
    # for some reason, these must
    # all exist, even if None
    'namespace'  : namespace,
    'passwd'     : 'users.txt',
    'sshPort'    : None,
    'telnetPort' : '4040',
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文