How To Fix the Python Error: TypeError: A Bytes-Like Object Is Required, Not ‘Str’ Socket

One of the great things about Python is that it can vastly simplify normally difficult feats of programming. Creating networked nodes, communicating over a client/server model, and a variety of other normally difficult tasks can be accomplished with just a few blocks of code. However, new functionality opens up the possibility of new errors. And this is where many people encounter socket errors for the first time. Likewise, it’s often how people first encounter a ” typeerror: a bytes-like object is required, not ‘str’ socket ” error message. But fixing the error is as simple as taking a closer look at how Python handles bytes-like objects and sockets.

What Does the TypeError Actually Mean?

TypeErrors refer to a conflict between the object type that’s expected and what is actually being used. It’s somewhat akin to using the wrong key in a lock. In this case, a function expects one of Python’s bytes-like objects but is instead being passed a string through socket.

A Closer Look at the Error

Sockets are how we send data throughout both internal and external networks. The Internet is one of the most obvious environments where you’ll see the end effect of socket use. Python’s socket API makes it easy to set up and use sockets. But there’s some aspects of sockets that are a little counterintuitive. One of the most obvious examples is the method used to send data through sockets. When people hear about transmitting information in Python they usually think of strings. After all, it’s what we use when we want to send a message to an output device.

However, it’s important to keep in mind that Python’s strings aren’t quite as simple as they might appear at first glance. Python’s strings are actually a collection of bytes encoded in Unicode. And this fact can make communication between different systems more complicated than you might expect. The Internet can essentially be a black box of hidden keys and values. And the processes that send and receive data don’t necessarily understand the contents of what they’re transmitting. It’s somewhat analogous to the postal system. Mail carriers don’t know what a letter or package might contain. But they know that items need to be properly enclosed and addressed before going into the mail system.

Likewise, when Python transmits data it doesn’t necessarily need to understand what any of it means. But the system will insist on that data being properly prepared. This means insistence on the byte-like object format when sending information through a socket. Remember that Python’s strings are simple to create, use, and manipulate within a Python script. But a web server doesn’t understand what Python strings are. As such, we need to convert any string sent through a socket into a bytes-like object. The following Python code shows what happens when we don’t convert a string before sending it to an open socket.

import socket
ourSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ourSocket.connect((‘www.google.com’, 80))
ourSocket.send(‘GET https://www.google.com/test.txt HTTP/1.0\n\n’)

while True:
    received = ourSocket.recv(1000)
    if ( len(received) < 1):
        break
    print (received);
ourSocket.close()

In this example, we begin by importing Python’s socket library and opening a connection to Google on port 80. We continue on to format a string that requests a nonexistent text file. Next, we create a while loop to receive and print out a limited amount of data in response to the socket’s transmission. Finally, the socket is closed and the program terminates. Or at least that’s how it’d go if things were working properly. We instead receive the ” typeerror: a bytes-like object is required, not ‘str’ socket ” Python error.

How To Fix the Error

The previous example gets almost everything right. But note how the socket send method is formed. It’s just a standard Python string. And, as we’ve seen, send needs to use a bytes-like object. But that problem is solvable, quite literally, with just a single character. Replace the ourSocket.send with the following line.

ourSocket.send(b’GET https://www.google.com/test.txt HTTP/1.0\n\n’)

The b before what had previously been a string tells python to instead create it as a bytes-like object. We now have the correct encoding to send the request through a socket. With that one change, the script will run cleanly and return the standard 404 feedback from Google.

How To Fix the Python Error: TypeError: A Bytes-Like Object Is Required, Not ‘Str’ Socket
Scroll to top