我有一个基于 Twisted 示例聊天服务器的服务器。它适用于远程登录。现在我想要一个客户端连接到它。我复制了示例一次性客户端。问题是服务器永远不会收到应该启动“对话”的“hello”字符串。我在常见问题解答中读到,一个常见的错误是阻止反应器运行,但我看不到在下面的代码中可以在哪里执行此操作。谁能告诉我出了什么问题?
from twisted.internet import reactor
from twisted.internet.protocol import Factory, Protocol
from twisted.internet.endpoints import TCP4ClientEndpoint
class Greeter(Protocol):
def sendMessage(self, msg):
self.transport.write("%s\n" % msg)
class GreeterFactory(Factory):
def buildProtocol(self, addr):
return Greeter()
def gotProtocol(p):
p.sendMessage("hello")
reactor.callLater(1, p.sendMessage, "/p2")
reactor.callLater(2, p.transport.loseConnection)
point = TCP4ClientEndpoint(reactor, "localhost", 8123)
d = point.connect(GreeterFactory())
d.addCallback(gotProtocol)
reactor.run()