【问题标题】:Twisted ignoring data sent from MUD Clients?扭曲忽略从 MUD 客户端发送的数据?
【发布时间】: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 客户端 gmudJMC 上进行了尝试。如果它很重要,我正在运行 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


    【解决方案1】:

    如果有人偶然发现这个问题有类似的问题,我将我的发现作为公认的答案,这样人们就不必像我一样追寻。

    我通过将我的 Twisted 协议中的分隔符值从“\r\n”(默认)更改为“\n”(这是我的 MUD 客户端发送的内容。这意味着在 Telnet 中,当你输入字符串:

    Hello, World
    

    您的应用程序将收到它:

    Hello, World\r
    

    您可能需要在服务器端进行数据清理以保持秩序井然。我的最终代码如下:

    导入twisted.scripts.twistd 从 twisted.protocols 导入基本 来自twisted.internet 导入协议,reactor 来自twisted.application 导入服务,互联网

    class MyChat(basic.LineReceiver):
        def __init__(self):
            self.delimiter = "\n"
    
        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)
    

    感谢大家的帮助。

    【讨论】:

    • 啊,我以为你用wireshark检查过,MUD客户端使用暂停或其他东西作为分隔符。那么这是一个干净的解决方案:D
    • 基本上您无法对客户端是否发送\n、\r、\n\r 或\r\n 做出任何假设。就我个人而言,我不会尝试使用 LineReceiver 来处理 Telnet(尤其不是 MUD 流量),因为它从根本上不是基于行的协议,尤其是当您考虑 IAC 序列、ANSI 颜色等时。
    • 那么您建议使用哪些 Twisted 组件?
    • Twisted 还包含 Telnet 协议的实现;请参阅 twistedmatrix.com/documents/10.1.0/api/…>。当然,这并不能避免当用户敲击回车时弄清楚你的 MUD 客户端正在发送什么的问题;这完全独立于 Telnet 协议。
    【解决方案2】:

    您确定 MUD 客户端在每行之后发送行结束字符吗? lineReceived 只会在行结束字符发送后调用。

    编辑:

    在这里我找到了LineReceiver 的 API 文档。您可以使用 dataReceived 方法来查看您是否真的获得了任何类型的数据。如果我记得你可以像 lineReceived 一样使用它。

    【讨论】:

    • 我不确定他们是不是。有没有办法可以手动将它添加到下一个的末尾来测试它?
    • 您可以使用 Wireshark 对其进行测试。您还可以使用其他 Receiver 基类。自从我阅读 Twisted API 文档以来已经有一段时间了,但我认为还有另一种方法可以在数据块上线后立即调用,您可以使用它来查看是否有任何数据进入。但是我真的已经快一年没有阅读文档了,所以我可能错了。
    • 您的回复有帮助,我让 MUD 客户端工作,但我添加的方法破坏了 Telnet 支持。我已在对我最初帖子的编辑中对其进行了概述。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多