Tôi cần kết nối với ứng dụng twisted khi đang chạy và tôi đang cố gắng để có được twisted.manhole để làm việc cho tôi đến đích đó. Tôi đang sử dụng Mac OSX 10.6 với phiên bản 8.2 được cài đặt theo mặc định.cửa cống xoắn: cách truy cập máy chủ trong ứng dụng?
sample server using twistd hoạt động. Có DeprecationWarnings lúc khởi động về MD5, SHA và twisted.protocols.telnet, nhưng máy chủ cửa cống thực sự làm những gì nó là vụ phải và tôi có thể truy cập vào bên trong của ứng dụng của tôi:
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>
>>>
Bây giờ tôi cố gắng hội nhập này vào ứng dụng của tôi:
# 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)
chạy đoạn mã trên trong một vỏ:
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>
trong một lớp vỏ thứ hai, chạy một máy khách 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
Vì vậy, có vẻ như đối tượng dịch vụ không thể sử dụng để truy cập nội bộ ứng dụng.
OK, vì twisted.protocols.telnet
nào dường như đã được thay thế bởi twisted.conch.telnet
, hãy thử sử dụng các mô-đun mới hơn:
# 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)
Các 'tập tin mật khẩu' users.txt
có thể chứa ít nhất là một dòng với một tên người dùng và mật khẩu mẫu, ví dụ admin:admin
.
Khởi động máy chủ thử nghiệm:
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>
Trong một lớp vỏ thứ hai, chạy một telnet client - lưu ý rằng một số những điều này thực sự là phỏng đoán như readline buggy Mac OSX (hoặc bất cứ điều gì khác là để đổ lỗi) có vẻ nuốt một số đầu ra vỏ:
host:client user$ telnet localhost 4040
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Username: admin
Password: *****
>>> dir()
['__builtins__']
Vì vậy, bây giờ có vẻ như tôi đã đi từ việc có một đối tượng vô dụng.
Twisted.manhole được sử dụng chính xác như thế nào?
Chỉ vì tò mò, mật khẩu có phải là "quản trị viên" không? –
có. tôi tiếp tục sử dụng cổng 4040, quản trị viên tên người dùng và quản trị mật khẩu. – ssc