【发布时间】:2011-12-17 02:06:09
【问题描述】:
我的目标是根据 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(...) 时,它会按预期运行。
我在这里做错了什么?
谢谢!
EDIT 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
EDIT 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的一个实例。工厂必须是
ClientFactory。如果我们查看twisted.internet.endoints的来源,我们会看到我们传递给端点的工厂被包裹在_WrappingFactory中,它是ClientFactory的后代。如果我们传入的这个工厂没有和ClientFactory相同的属性,那么_wrappedFactory在尝试调用ClientFactory的方法时会导致AttributeErrors
【问题讨论】: