2011-08-24 41 views
5

我需要在运行时连接到我的twisted应用程序,并且我试图让twisted.manhole为此工作。我在Mac OSX 10.6上默认安装了扭曲的8.2。扭曲的沙井:如何在应用程序中访问服务器?

sample server using 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) 

运行上面的代码中的壳:

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> 

在第二壳中,运行一个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 

所以看起来服务对象不可用于访问应用程序的内部。

OK,因为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> 

在第二外壳,运行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如何正确使用?

+1

出于好奇,密码是“admin”吗? –

+0

是的。我继续使用端口4040,用户名管理员和密码管理员。 – ssc

回答

5

继续使用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', 
} 
相关问题