TypeError: 'str' does not support the buffer interface using Python3.x

I have just migrate from python 2.x to python 3.x and the code below has stopped working.

    #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

Any idea how to fix this? thanks!

Upvotes: 0

Views: 1249

Answers (1)

gabhijit
gabhijit

Reputation: 3585

This happens because bytes in python 3 are not same as str. What the request.recv gives you is a bytes of data. You'd need to first convert it to str and then use it to split.

You can do an utf-8 decode. So something like -

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

How to convert between bytes and strings in Python 3? has a more detailed explanation.

Upvotes: 1

Related Questions