使用Python Tubes和Twisted模块获取TypeError

问题描述 投票:0回答:1

我目前正在关注documentation for Python Tubes,大部分情况下一切都很好。问题是,一旦我在代码中实现了Reverser类,当我尝试通过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模块处理数据的简单示例一样。对于我的一生,我不知道为什么它不起作用。我尝试过在线查找,但似乎找不到解决方案。到底是什么原因造成的?

我目前正在关注Python Tubes的文档,并且大多数情况下一切进展顺利。问题是,一旦我在代码中实现了Reverser类,当我...

python networking twisted tubes
1个回答
1
投票

[文档渲染器中似乎存在错误。如果您阅读the source listing,则会看到Reverser应该用tube装饰:

© www.soinside.com 2019 - 2024. All rights reserved.