TypeError:' str'不支持使用Python3.x的缓冲区接口

时间:2015-06-23 21:02:37

标签: sockets python-3.x

我刚刚从python 2.x迁移到python 3.x,下面的代码已停止工作。

    #self.request is the TCP socket connected to the client
    self.data = self.request.recv(1024).strip()
    print "{} wrote:".format(self.client_address[0])
    print self.data

    words = self.data.split(',') //the problem seems to be here

知道怎么解决这个问题吗?谢谢!

1 个答案:

答案 0 :(得分:1)

这是因为python 3中的bytesstr不同。 request.recv为您提供的是bytes数据。您需要先将其转换为str,然后将其用于split

您可以执行utf-8解码。所以像 -

self.data.decode('utf-8').split(',') should work. 

it uses the 64bit ABI有更详细的解释。