我有一个自定义协议处理程序,该处理程序处理以python2编写的传入消息。数据以使用构造库的编码字节的形式出现,应进行检查并下传给构造解析器。
但是在执行其他任何操作之前,带有“ builtins.TypeError:必须是str而不是字节”的dataReceived函数错误。我尝试将数据存储定义为空字节数组,然后将传入的数据添加到其中,但是没有任何效果。在python终端上独立运行时,代码管道有效,因此我不知道问题出在哪里。
完整跟踪:
2019-07-19 09:13:06+0300 [SimpleMessageProtocol,0,127.0.0.1] data received
2019-07-19 09:13:06+0300 [SimpleMessageProtocol,0,127.0.0.1] Unhandled Error
Traceback (most recent call last):
File "/home/joonas/.local/lib/python3.6/site-packages/twisted/python/log.py", line 103, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/home/joonas/.local/lib/python3.6/site-packages/twisted/python/log.py", line 86, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/home/joonas/.local/lib/python3.6/site-packages/twisted/python/context.py", line 122, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/home/joonas/.local/lib/python3.6/site-packages/twisted/python/context.py", line 85, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/home/joonas/.local/lib/python3.6/site-packages/twisted/internet/posixbase.py", line 614, in _doReadOrWrite
why = selectable.doRead()
File "/home/joonas/.local/lib/python3.6/site-packages/twisted/internet/tcp.py", line 243, in doRead
return self._dataReceived(data)
File "/home/joonas/.local/lib/python3.6/site-packages/twisted/internet/tcp.py", line 249, in _dataReceived
rval = self.protocol.dataReceived(data)
File "/usr/local/lib/python3.6/dist-packages/simple_message-0.0.1-py3.6.egg/simple_message/protocol.py", line 103, in dataReceived
builtins.TypeError: must be str, not bytes
和dataReceived函数:
def dataReceived(self, data):
log.msg("data received")
log.msg(data)
self._remainingData.extend(data)
while self._remainingData:
try:
self._consumeData()
except IncompleteMessageException:
# try again later (when we get more data)
break
该项目很大,可以在这里找到:https://github.com/Roiki-design/simple_message_py
修改
它已解决,显然旧的扭曲日志无法像我在python 3中预期的那样工作。log.msg似乎只需要字符串。