2011-08-24 5 views
5

私は実行時に私のtwistedアプリケーションに接続する必要があり、私はtwisted.manholeをその目的のために働かせようとしています。私は、デフォルトでインストールされているように、Mac OSX 10.6でひねり8.2を使用しています。ねじれたマンホール:アプリケーション内のサーバにアクセスするには?

sample server using twistdが動作します。そこMD5、SHAとtwisted.protocols.telnetに関する起動時のがDeprecationWarningがありますが、マンホールサーバは、実際にそれがすることになっていると私は自分のアプリケーションの内部にアクセスできるかを行います。

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> 

を第2のシェルでは、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に取って代わられているようだから、新しいモジュール使用してみてください:「パスワードファイル」users.txt

# 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) 

は、サンプルのユーザー名とパスワードを使用して、わずか1行が含まれていてもよいです例えばadmin:admin。思われる(または非難する任意の他である)、こののいくつかは、実際にバグがマックOSX用のreadlineとして当て推量であることに注意してください - telnetクライアントを実行し、第2のシェルでは

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> 

は、テストサーバーを起動しますいくつかのシェル出力飲み込む:

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、ユーザー名adminとパスワードadminを使い続けています。 – 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', 
} 
関連する問題