【问题标题】:Thrift python 3.4 TypeError: string argument expected, got 'bytes'Thrift python 3.4 TypeError:需要字符串参数,得到'字节'
【发布时间】:2015-10-30 09:39:05
【问题描述】:

我正在尝试使用 Thrift 似乎支持的 python 3.4 Apache Thrift,因为它有 lib.linux-x86_64-3.4 在build 目录下。但我不断收到此错误消息

  File "/home/qunzi/Downloads/thrift-0.9.2/lib/py/build/lib.linux-x86_64-3.4/thrift/transport/TTransport.py", line 163, in write
self.__wbuf.write(buf)
TypeError: string argument expected, got 'bytes'

有人知道发生了什么,并且可能有解决方案吗?

下面是相关代码

socket = TSocket.TSocket('localhost', 9090)
transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = Algo.Client(protocol)
transport.open()
ping_req = PingRequest()
ping_resp = client.ping(ping_req)

整个 Traceback:

  File "py3client.py", line 36, in <module>
    ping_resp = client.ping(ping_req)
  File "/home/qunzi/Projects/test/sample_Test/py3.4_thrift/ib/Algo.py", line 66, in ping
    self.send_ping(request)
  File "/home/qunzi/Projects/test/sample_Test/py3.4_thrift/ib/Algo.py", line 70, in send_ping
    self._oprot.writeMessageBegin('ping', TMessageType.CALL, self._seqid)
  File "/home/qunzi/Downloads/thrift-0.9.2/lib/py/build/lib.linux-x86_64-3.4/thrift/protocol/TBinaryProtocol.py", line 46, in writeMessageBegin
    self.writeI32(TBinaryProtocol.VERSION_1 | type)
  File "/home/qunzi/Downloads/thrift-0.9.2/lib/py/build/lib.linux-x86_64-3.4/thrift/protocol/TBinaryProtocol.py", line 111, in writeI32
    self.trans.write(buff)
  File "/home/qunzi/Downloads/thrift-0.9.2/lib/py/build/lib.linux-x86_64-3.4/thrift/transport/TTransport.py", line 163, in write
    self.__wbuf.write(buf)
TypeError: string argument expected, got 'bytes'

【问题讨论】:

  • 在什么情况下会收到这个错误?这是完整的痕迹吗?
  • 不,这不是完整的跟踪。我只是简单地调用ping,它们在thrift 文件中定义为空结构struct PingRequest {}、struct PingResponse {} 和PingResponse ping (1:required PingRequest request);
  • string 和 binary 在 Thrift 中有很多共同点,主要是出于历史原因。这仍然不能解释问题,但缩小了范围。如果没有人提出解决方案或建议,我建议将其放入邮件列表。也许你会在那里得到答案。

标签: python python-3.4 thrift thrift-protocol


【解决方案1】:

虽然这个问题已经死了,但看起来我有一个答案:)

thrift 的当前 python 生成器(从 0.9.3 开始)生成特定于 python2 的代码。至少我刚刚切换了齿轮和给我同样错误的代码,现在就像一个魅力。

该错误是由(隐式)将库中的所有类似字符串的对象视为字节对象引起的。但是,在 Python3 中,StringIO 类需要面向字符的字符串 (str),而那些面向字节的字符串会导致异常。

应该向 Thrift 跟踪器提交错误,现在他/她应该改用 Python2。

【讨论】:

    猜你喜欢
    • 2020-11-04
    • 2019-03-01
    • 1970-01-01
    • 2021-05-10
    • 2021-08-22
    • 2018-05-18
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    相关资源
    最近更新 更多