Thrift python 3.4 TypeError:期望的字符串参数,获得'bytes'

时间:2015-08-07 03:39:56

标签: python python-3.4 thrift thrift-protocol

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

  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)

整个追溯:

  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'

1 个答案:

答案 0 :(得分:1)

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

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

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

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