使用 Python Tubes 和 Twisted 模块获取 TypeError

Getting TypeError with Python Tubes and Twisted module

我目前正在关注 documentation for Python Tubes,大部分情况下一切顺利。问题是,一旦我将 Reverser class 实现到我的代码中,当我尝试通过 Telnet 连接时,TypeError 不断出现。

这是我的代码:

#!/etc/python2.7
from tubes.protocol import flowFountFromEndpoint
from tubes.listening import Listener

from twisted.internet.endpoints import serverFromString
from twisted.internet.defer import Deferred, inlineCallbacks
from twisted.python import log

from tubes.framing import bytesToLines, linesToBytes
from tubes.tube import series



class Reverser(object):
    def received(self, item):
        yield b"".join(reversed(item))

def reverseFlow(flow):
    lineReverser = series(bytesToLines(), Reverser(), linesToBytes())
    flow.fount.flowTo(lineReverser).flowTo(flow.drain)

@inlineCallbacks
def main(reactor, listenOn="stdio:"):
    listener = Listener(reverseFlow)
    endpoint = serverFromString(reactor, listenOn)
    flowFount = yield flowFountFromEndpoint(endpoint)
    flowFount.flowTo(listener)
    yield Deferred()

if __name__ == '__main__':
    from twisted.internet.task import react
    react(main, ["tcp:8000"])

我正在另一个终端(Linux、Ubuntu 19.10)中使用此命令连接到上面显示的服务器:

telnet 127.0.0.1 8000

第二次尝试连接时,服务器显示此消息:

Unhandled Error
Traceback (most recent call last):
  File "/home/edgecase/.local/lib/python2.7/site-packages/twisted/python/log.py", line 86, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/home/edgecase/.local/lib/python2.7/site-packages/twisted/python/context.py", line 122, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/home/edgecase/.local/lib/python2.7/site-packages/twisted/python/context.py", line 85, in callWithContext
    return func(*args,**kw)
  File "/home/edgecase/.local/lib/python2.7/site-packages/twisted/internet/posixbase.py", line 614, in _doReadOrWrite
    why = selectable.doRead()
--- <exception caught here> ---
  File "/home/edgecase/.local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 1435, in doRead
    protocol.makeConnection(transport)
  File "/home/edgecase/.local/lib/python2.7/site-packages/twisted/internet/protocol.py", line 514, in makeConnection
    self.connectionMade()
  File "/home/edgecase/.local/lib/python2.7/site-packages/tubes/protocol.py", line 239, in connectionMade
    self._flow(self._fount, self._drain)
  File "/home/edgecase/.local/lib/python2.7/site-packages/tubes/protocol.py", line 411, in aFlowFunction
    listening.impl.drain.receive(Flow(fount, drain))
  File "/home/edgecase/.local/lib/python2.7/site-packages/tubes/listening.py", line 93, in receive
    item.drain))
  File "/home/edgecase/Programs/Tubes/test3.py", line 19, in reverseFlow
    lineReverser = series(bytesToLines(), Reverser(), linesToBytes())
  File "/home/edgecase/.local/lib/python2.7/site-packages/tubes/tube.py", line 235, in series
    drains = [IDrain(tube) for tube in tubes]
exceptions.TypeError: ('Could not adapt', <__main__.Reverser object at 0x7faf92066a90>, <InterfaceClass tubes.itube.IDrain>)

理想情况下,服务器应接收发送给它的任何数据,反转该数据并将其发回 - 就像使用 Tubes 模块处理数据的简单示例一样。对于我的一生,我无法弄清楚为什么它不起作用。我试过在线查看,但似乎找不到解决方案。 究竟是什么原因造成的?

文档呈现器中似乎存在错误。如果你阅读 the source listing 那么你会看到 Reverser 应该用 tube:

装饰
from tubes.tube import tube, series

@tube
class Reverser(object):
    def received(self, item):
        yield b"".join(reversed(item))

我希望这个装饰器做出必要的注释,以便 Reverser 个实例可以适应 IDrain