如何使用 tctelnet 构建端点
我的目标是根据 TCP4ClientEndpoint
实现创建 telnet 客户端作为端点。
这就是我正在做的事情:
class TelnetClient( TelnetProtocol ):
...
factory = Factory()
factory.protocol = TelnetClient
point = TCP4ClientEndpoint( reactor, x.x.x.x, 23 )
defer = point.connect( factory )
defer.addCallback( todo )
reactor.run
TelnetClient
类处理身份验证、登录、触发命令等。
当我使用这种方法时,我可以从 dataReceived
读取一些输出,但它是胡言乱语。
当 telnet 客户端由 Factory
构造时,它会按预期运行,然后使用 Factory
调用 reactor.connectTCP(...)
。
我在这里做错了什么?
谢谢!
编辑1通过TelnetProtocol
将TelnetClient
连接到factory.protocol
class TelnetClient( TelnetProtocol ):
...
factory = Factory()
factory.protocol = TelnetTransport( TelnetClient )
point = TCP4ClientEndpoint( reactor, x.x.x.x, 23 )
defer = point.connect( factory )
defer.addCallback( todo )
reactor.run
编辑2已解决。最后一块是 ClientFactory。
class TelnetClient( TelnetProtocol ):
...
factory = ClientFactory()
factory.protocol = TelnetTransport( TelnetClient )
point = TCP4ClientEndpoint( reactor, x.x.x.x, 23 )
defer = point.connect( factory )
解决这个问题有两个方面。
由于我们需要一个 telnet 客户端,因此我们需要确保该协议是
TelnetProtocol
的实例。工厂必须是
的方法时会导致AttributeErrors。ClientFactory
。如果我们查看twisted.internet.endoints
的源代码,我们会发现我们传递给端点的工厂被包装在_WrappingFactory
中,它是的后代。客户端工厂
。如果我们传入的这个工厂与ClientFactory
不具有相同的属性,那么_wrappedFactory
在尝试调用ClientFactory
My goal is to create telnet clients as endpoints per the TCP4ClientEndpoint
implementation.
Here's what Im doing:
class TelnetClient( TelnetProtocol ):
...
factory = Factory()
factory.protocol = TelnetClient
point = TCP4ClientEndpoint( reactor, x.x.x.x, 23 )
defer = point.connect( factory )
defer.addCallback( todo )
reactor.run
The TelnetClient
class handles authentication, logging in, firing commands, etc.
when I use this approach, I can read some output off of dataReceived
, but it's giberish.
The telnet client functions as expected when it is constructed by a Factory
and then reactor.connectTCP(...)
is called with the Factory
.
What is it that Im doing wrong here?
Thanks!
EDIT 1 connecting TelnetClient
to factory.protocol
via TelnetProtocol
class TelnetClient( TelnetProtocol ):
...
factory = Factory()
factory.protocol = TelnetTransport( TelnetClient )
point = TCP4ClientEndpoint( reactor, x.x.x.x, 23 )
defer = point.connect( factory )
defer.addCallback( todo )
reactor.run
EDIT 2 solved. The final piece was ClientFactory.
class TelnetClient( TelnetProtocol ):
...
factory = ClientFactory()
factory.protocol = TelnetTransport( TelnetClient )
point = TCP4ClientEndpoint( reactor, x.x.x.x, 23 )
defer = point.connect( factory )
Solving this problem was two fold.
Since we want a telnet client, we need to ensure that the protocol is an instance of
TelnetProtocol
.The factory must be of
ClientFactory
. If we look at the source oftwisted.internet.endoints
, we see that the factory we pass in to the endpoints is wrapped in_WrappingFactory
, which is descended fromClientFactory
. If this factory we pass in does not have the same attributes asClientFactory
, then the_wrappedFactory
will cause AttributeErrors when it attempts to call methods of ClientFactory
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是对的,
connectTCP
和endpoint.connect
在功能上是相同的(大部分)。假设
TelnetProtocol
是twisted.conch.telnet.TelnetProtocol
,这里的问题是TelnetProtocol
并不真正应该直接连接到 TCP 传输,它应该连接到twisted.conch.telnet.TelnetTransport
。您看到的 dataReceived 中的“乱码”是实际的 telnet 协议字节,应该由twisted.conch.telnet.TelnetTransport 解析(即本身是一个IProtocol
),以便调用TelnetTransport
上的enableLocal
和enableRemote
等方法。我猜想在基于
connectTCP
的示例中,您可能正在实例化TelnetTransport
并设置其.protocol
属性指向TelnetProtocol
。基本上,请确保您传入的
Factory
对象与ClientFactory
具有完全相同相同的protocol
属性> 您在connectTCP
示例中使用。将来,还请提供完整的、可运行的代码示例,以便我们可以运行它们并看看会发生什么,而不是猜测:-)。
You're correct that
connectTCP
andendpoint.connect
are functionally the same (for the most part).Assuming that
TelnetProtocol
istwisted.conch.telnet.TelnetProtocol
, the problem here is thatTelnetProtocol
is not really supposed to connect directly to a TCP transport, it's supposed to connect to atwisted.conch.telnet.TelnetTransport
. That "gibberish" indataReceived
that you're seeing are the actual telnet protocol bytes, which are supposed to be parsed by atwisted.conch.telnet.TelnetTransport
(which is itself anIProtocol
) in order to call methods likeenableLocal
andenableRemote
on theTelnetTransport
.I would guess that in your
connectTCP
-based example, you are probably instantiating aTelnetTransport
and setting its.protocol
attribute to point at aTelnetProtocol
.Basically, make sure that the
Factory
object that you're passing in has exactly the sameprotocol
attribute as theClientFactory
you're using in yourconnectTCP
example.In the future, also, please include complete, runnable code examples so that we can run them and see what happens rather than guessing :-).