如何使用 tctelnet 构建端点

发布于 2024-12-12 12:25:41 字数 1692 浏览 0 评论 0原文

我的目标是根据 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通过TelnetProtocolTelnetClient连接到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 )

解决这个问题有两个方面。

  1. 由于我们需要一个 telnet 客户端,因此我们需要确保该协议是 TelnetProtocol 的实例。

  2. 工厂必须是ClientFactory。如果我们查看 twisted.internet.endoints 的源代码,我们会发现我们传递给端点的工厂被包装在 _WrappingFactory 中,它是 的后代。客户端工厂。如果我们传入的这个工厂与ClientFactory不具有相同的属性,那么_wrappedFactory在尝试调用ClientFactory

    的方法时会导致AttributeErrors。

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.

  1. Since we want a telnet client, we need to ensure that the protocol is an instance of TelnetProtocol.

  2. The factory must be of ClientFactory. If we look at the source of twisted.internet.endoints, we see that the factory we pass in to the endpoints is wrapped in _WrappingFactory, which is descended from ClientFactory. If this factory we pass in does not have the same attributes as ClientFactory, then the _wrappedFactory will cause AttributeErrors when it attempts to call methods of ClientFactory

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

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

发布评论

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

评论(1

红颜悴 2024-12-19 12:25:41

您是对的,connectTCPendpoint.connect 在功能上是相同的(大部分)。

假设 TelnetProtocoltwisted.conch.telnet.TelnetProtocol,这里的问题是 TelnetProtocol 并不真正应该直接连接到 TCP 传输,它应该连接到 twisted.conch.telnet.TelnetTransport。您看到的 dataReceived 中的“乱码”是实际的 telnet 协议字节,应该由twisted.conch.telnet.TelnetTransport 解析(即本身是一个 IProtocol),以便调用 TelnetTransport 上的 enableLocalenableRemote 等方法。

我猜想在基于 connectTCP 的示例中,您可能正在实例化 TelnetTransport 并设置 .protocol属性指向 TelnetProtocol

基本上,请确保您传入的 Factory 对象与 ClientFactory 具有完全相同相同的 protocol 属性> 您在 connectTCP 示例中使用。

将来,还请提供完整的、可运行的代码示例,以便我们可以运行它们并看看会发生什么,而不是猜测:-)。

You're correct thatconnectTCP and endpoint.connect are functionally the same (for the most part).

Assuming that TelnetProtocol is twisted.conch.telnet.TelnetProtocol, the problem here is that TelnetProtocol is not really supposed to connect directly to a TCP transport, it's supposed to connect to a twisted.conch.telnet.TelnetTransport. That "gibberish" in dataReceived that you're seeing are the actual telnet protocol bytes, which are supposed to be parsed by a twisted.conch.telnet.TelnetTransport (which is itself an IProtocol) in order to call methods like enableLocal and enableRemote on the TelnetTransport.

I would guess that in your connectTCP-based example, you are probably instantiating a TelnetTransport and setting its .protocol attribute to point at a TelnetProtocol.

Basically, make sure that the Factory object that you're passing in has exactly the same protocol attribute as the ClientFactory you're using in your connectTCP example.

In the future, also, please include complete, runnable code examples so that we can run them and see what happens rather than guessing :-).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文