Skip to content
Advertisement

Python Error: ‘TypeError: a bytes-like object is required, not ‘str” for Robot Controller using UDP Protocol

I was trying to make a keyboard-based controller for my robot using the UDP protocol to send packets of keypresses to an ESP8266 NodeMCU microcontroller using Python. When I wrote this code, the following error appeared:

JavaScript

This is the code:

JavaScript

Advertisement

Answer

You got this error because the method you’re calling expects a bytes object. This makes sense, since UDP packets consists of bytes. They do not have any notion of the meaning of those bytes, i.e. if they are text or images or something else.

If you want to send text using a byte-based protocol, you have to encode it.

In your case, since you seem to be sending one character at a time and you’re using string literals, the easiest solution is to add a b before the letters you want to send, so b'u' instead of 'u':

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement