Python Twisted:使用 DataReceived 接收的内容以及我可能应该使用的内容

Python Twisted: What Is Received with DataReceived and What Should I Probably Use Instead

我正在使用 Twisted 在 Python 中开发一款基于文本的冒险游戏,我想我从根本上误解了 Twisted 的 dataReceived 函数返回的结果。我的整体代码中包含以下内容;

def process(self, data):
    print "Got command {}, {}".format(data, len(data))
    if data == "test":
        print "DEBUG: got the test command"


from twisted.internet import reactor, protocol

class MudLoop(protocol.Protocol):

  def connectionMade(self):
    login(self)

  def dataReceived(self, data):
    process(self, data)


factory = protocol.ServerFactory()
factory.protocol = MudLoop
reactor.listenTCP(12000,factory)
reactor.run()

从 dataReceived 发回的字符串都不会触发流程函数对其进行测试并执行其他操作,例如如果已连接的客户端输入 'test',根据 DEBUG 指令,不会收到控制台输出,表示它已收到测试命令。关于为什么,我有几个理论,第一个是传递的不是字符串,第二个是虽然它是一个字符串,但它有不可见的字符(回车符 return 和换行符?)使它无法通过输入内容的测试 - 这似乎得到了我对正在调用的过程函数的测试的支持,在控制台中的新行中添加命令的长度。

以下哪些是正确的?或者是别的什么?在任何情况下,我都应该以这种方式测试来自 dataReceived 的字符串 returned 吗?在我发布之前进行的研究似乎表明其他人没有遇到过这个问题,但后来 运行 对其他问题进行了研究,其中字符串以不确定的顺序从多个客户端接收。如果我解决了这个问题,那我的方法就错了,结果我只是继续下一个问题?

这是一个扭曲的常见问题解答:Why is dataReceived called with only part of the data I called transport.write with?