Thrift python 3.4 TypeError:预期的字符串参数,得到“字节" [英] Thrift python 3.4 TypeError: string argument expected, got 'bytes'

查看:76
本文介绍了Thrift python 3.4 TypeError:预期的字符串参数,得到“字节"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying out Apache Thrift using python 3.4 which Thrift seems to have support, since it has lib.linux-x86_64-3.4 under build directory. But I am keeping 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'

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

Anybody knows what's going on, and possibly with a solution?

以下是相关代码

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'

推荐答案

虽然这个问题很死,但看起来我有答案:)

Although the question is rather dead, it looks like I have an answer :)

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

The current python generator of thrift (as of 0.9.3) generates code, specific to python2. At least I've just switched the gears and the code which gave me the same error, now works like a charm.

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

The error is caused by (implicit) treating of all string-like objects within the library like byte objects. However, in Python3 StringIO class expects char-oriented strings (str) and those byte-oriented strings cause the exception.

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

One should file a bug to the Thrift tracker, and for now he/she should use Python2 instead.

这篇关于Thrift python 3.4 TypeError:预期的字符串参数,得到“字节"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆