使用Python Tube和Twisted模块获取TypeError
我目前正在跟踪调查,大部分情况下进展顺利。问题是,一旦我在代码中实现了使用Python Tube和Twisted模块获取TypeError,python,networking,twisted,tubes,Python,Networking,Twisted,Tubes,我目前正在跟踪调查,大部分情况下进展顺利。问题是,一旦我在代码中实现了Reverser类,当我试图通过Telnet连接时,一个类型错误不断出现 这是我的密码: #!/etc/python2.7 from tubes.protocol import flowFountFromEndpoint from tubes.listening import Listener from twisted.internet.endpoints import serverFromString from twist
Reverser
类,当我试图通过Telnet连接时,一个类型错误不断出现
这是我的密码:
#!/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>)
未处理的错误
回溯(最近一次呼叫最后一次):
callWithContext中的文件“/home/edgecase/.local/lib/python2.7/site packages/twisted/python/log.py”,第86行
返回context.call({ILogContext:newCtx},func,*args,**kw)
callWithContext中的第122行文件“/home/edgecase/.local/lib/python2.7/site packages/twisted/python/context.py”
返回self.currentContext().callWithContext(ctx,func,*args,**kw)
callWithContext中第85行的文件“/home/edgecase/.local/lib/python2.7/site packages/twisted/python/context.py”
返回函数(*参数,**kw)
文件“/home/edgecase/.local/lib/python2.7/site-packages/twisted/internet/posixbase.py”,第614行,在
why=selectable.doRead()
--- ---
文件“/home/edgecase/.local/lib/python2.7/site packages/twisted/internet/tcp.py”,第1435行,在doRead中
协议.makeConnection(传输)
文件“/home/edgecase/.local/lib/python2.7/site packages/twisted/internet/protocol.py”,第514行,在makeConnection中
self.connectionMade()
文件“/home/edgecase/.local/lib/python2.7/site packages/tubes/protocol.py”,第239行,在ConnectionMode中
自流量(自流量、自排水)
文件“/home/edgecase/.local/lib/python2.7/site packages/tubes/protocol.py”,第411行,在aFlowFunction中
监听.输入排水.接收(流量(流量,排水))
文件“/home/edgecase/.local/lib/python2.7/site packages/tubes/listening.py”,第93行,接收
项目1(排水管)
文件“/home/edgecase/Programs/Tubes/test3.py”,第19行,反向流
lineReverser=系列(bytesToLines()、Reverser()、linesToBytes())
文件“/home/edgecase/.local/lib/python2.7/site packages/tubes/tube.py”,第235行,串联
排水管=[IDrain(管)用于管中管]
exceptions.TypeError:(“无法调整”,)
理想情况下,服务器应该接收发送给它的任何数据,反转该数据并将其发送回——就像使用Tubes模块处理数据的简单示例一样。就我的一生而言,我不明白为什么它不起作用。我试过上网,但似乎找不到解决办法。
究竟是什么原因导致了这种情况?文档渲染器中似乎有一个bug。如果您阅读,您会看到反向器应该用管装饰:
from tubes.tube import tube, series
@tube
class Reverser(object):
def received(self, item):
yield b"".join(reversed(item))
我希望这个装饰器能够做出必要的注释,以便
Reverser
实例能够适应IDrain
总是一些小事情让你开心