Thrift python 3.4 TypeError: expected string argument received by 'bytes'
I'm trying Apache Thrift using python 3.4, which seems to have support since it has lib.linux-x86_64-3.4
a directory build
. But I keep getting this error message
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'
Does anyone know what is going on and maybe a solution?
Below is the relevant code
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)
the whole 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'
source to share
Although the question is pretty dead, it looks like I have an answer :)
The current lean python generator (as of 0.9.3) generates python2-specific code. At least I just changed gears and the code that gave me the same error now works like a charm.
The error is caused by the (implicit) handling of all string objects in the library, such as byte objects. However, in Python3, the StringIO class expects char-oriented strings ( str
), and these byte-oriented strings throw an exception.
Thrift tracker needs to report an error and should now use Python2 instead.
source to share