【发布时间】:2009-12-14 00:49:12
【问题描述】:
我有以下代码(几乎是 here 列出的聊天服务器示例的精确副本:
导入twisted.scripts.twistd 从 twisted.protocols 导入基本 来自twisted.internet 导入协议,reactor 来自twisted.application 导入服务,互联网
class MyChat(basic.LineReceiver):
def connectionMade(self):
print "Got new client!"
self.factory.clients.append(self)
def connectionLost(self, reason):
print "Lost a client!"
self.factory.clients.remove(self)
def lineReceived(self, line):
print "received", repr(line)
for c in self.factory.clients:
c.message(line)
def message(self, message):
self.transport.write(message + '\n')
factory = protocol.ServerFactory()
factory.protocol = MyChat
factory.clients = []
if __name__ == "__main__":
print "Building reactor...."
reactor.listenTCP(50000, factory)
print "Running ractor...."
reactor.run()
else:
application = service.Application("chatserver")
internet.TCPServer(50000, factory).setServiceParent(application)
服务器运行没有错误,如果我通过 Telnet 连接到它,我可以发送数据,服务器打印到控制台并将其中继到所有客户端(如预期的那样)。但是,如果我通过不同的工具(MUD 客户端)连接到它,它永远不会获取数据。
我已确保客户端正在发送数据(使用 Wireshark 跟踪数据包,并且它们正在通过网络传输),但服务器要么从未收到它,要么出于某种原因选择忽略它。
我已经在两个 MUD 客户端 gmud 和 JMC 上进行了尝试。如果它很重要,我正在运行 Windows 7 x64。
有人知道为什么会发生这种情况吗?
谢谢,
迈克
编辑:
感谢Maiku Mori 提供的提示,我尝试添加Twisted API Docs 中指定的另一种方法,dataReceived。添加后,MUD 客户端运行良好,但 Telnet 现在将每个字符作为其自己的数据集发送,而不是等待用户按 Enter。
这是新代码的片段:
def dataReceived(self, data):
print "Dreceived", repr(data)
for c in self.factory.clients:
c.message(data)
# def lineReceived(self, line):
# print "received", repr(line)
# for c in self.factory.clients:
# c.message(line)
以前有没有人经历过这种情况,如果有,您是如何解决的?理想情况下,我希望 Telnet 和 MUD 客户端使用此应用程序。
再次感谢。
【问题讨论】:
标签: python networking sockets tcp twisted