【问题标题】:Getting TypeError with Python Tubes and Twisted module使用 Python Tubes 和 Twisted 模块获取 TypeError
【发布时间】:2020-04-10 11:45:11
【问题描述】:

我目前正在关注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 networking twisted tubes


    【解决方案1】:

    文档渲染器中似乎存在错误。如果您阅读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

    【讨论】:

    • 得到的总是小东西​​哈哈
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 1970-01-01
    相关资源
    最近更新 更多