Python sockets error TypeError: a bytes-like object is required, not ‘str’

Python sockets error TypeError: a bytes-like object is required, not ‘str’ is an error which occurs in Python because in Python 3, strings are Unicode.

In this article we are going to solve this error together while trying to understand why the error happens, other solutions may work for you, so I am going to include the ones that are more likely to solve your particular issue.

Describing Python sockets error TypeError: a bytes-like object is required, not ‘str’

Python sockets error TypeError: a bytes-like object is required, not ‘str’ with send function

The reason for this error is that in Python 3, strings are Unicode, but when transmitting on the network, the data needs to be bytes instead. So… a couple of suggestions:

This error is a Python sockets error which happens when a problem happens in the information sent via the sockets. The error can occur when you are trying trying to send non bytes-like data on the network, which cannot work since on Python 3 the network can only transmit bytes.

Here is an example of a code that will cause this error to take place.

                                                                       #
soc = socket.socket()
host = '127.0.0.1'
port = 30000
soc.bind((host, port))

soc.listen(5)
while True:
    c, addr = soc.accept()
    print('connected from', addr)
    c.send('done')
    c.close()
                                                                       #

The command bellow is supposed to open a netcut connection, instead we get an error.

                                                                       #
nc 127.0.0.1 30000
                                                                       #

Here is the message of the resulting error.

                                                                       #
TypeError: a bytes-like object is required, not 'str'
                                                                       #

Solution 1 : decode the data with UTF-8

Since this error is a data type error, the solution is to get the data type right.

The network only accepts bytes or bytes-like data, so the fix is to decode the data using UTF-8, just like in the example bellow.

                                                                       #
receive.decode('utf_8')
                                                                       #

If this solution solves your problem, consider donating using the red Kofi donation button located at the top of this page.

If it does not, please continue reading, the solution bellow might work for you.

Solution 2 : An Awesome Fix

If you are sending a string literal with a socket the solution above will not work for you.

Instead you should add a b before the data, just like in the example bellow.

                                                                       #
c.send(b'connection successful')
                                                                       #

We can improve on the code above by replacing send with sendall in order to send all the data in one go.

                                                                       #
c.sendall(b'connection successful')
                                                                       #

Summing-up

The solutions above should be enough to solve your problem, I hope the article helped you get rid of the issue, please keep learning, keep coding and cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/