0

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!

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.