String to Bytes error Python: Solved
My solution for the Argument of type str cannot be assigned to type bytes|bytearray error in python
Last updated
My solution for the Argument of type str cannot be assigned to type bytes|bytearray error in python
Last updated
TL;DR- Python needs data in bytes format whenever it sends/receives data over a connection. Thus, using str(e)
throws an error. To solve this, just change the data type from string to bytes. A great way to do this is by converting the string to bytes and specifying the encoding to make your life easier. You can do this with bytes(str(e), encoding='utf-8')
While writing a python script that executes commands on a SSH server, I ran into a unique error. The script established connection with an SSH server, received commands and executed them. I also added a try-except block. Here's where python thew an error:
The error said [Argument of type "str" cannot be assigned to parameter "s" of type "bytes|bytearray" in function "send"]
To solve this error, we first have to understand what it means. We define a try-except block in python to deal with any errors that might arise while script execution. Python will first execute the try block and if there is an error, it will execute the except block. If a try-except block is not defined, the program will crash and raise an error. In this particular scenario, python is catching any errors in the script as the variable 'e' [except Exception as e] and then sending this particular variable to the server [ssh_session.send]. One difference here is that we are sending the error as a string [str(e)]. The rest of the code works fine but python is having difficulties with the string conversion. Now we know the location of this error, let's find out the reason.
To find out the reason, let's first understand what the overall code is doing. In my case, python is first connecting to an SSH server then sending/receiving data. Any data sent or received through python is in the form of bytes. Therefore, whenever we have to parse the received data in a human-readable format we use python's built-in decode()
method.
The problem here is that I am trying to send the 'e' variable as a string type when python needs bytes. Keep in mind that this problem might not arise in other scenarios because data in bytes format may not be necessary. Let's go back to the error message. [Argument of type "str" cannot be assigned to parameter "s" of type "bytes|bytearray" in function "send"]. Decode this one at a time. In non-coding language, python cannot work with a string here because the "send" function requires bytes.
Now, we know the location and the reason of this error, solving it is a piece of cake. The only thing I have to do here is to change the data format from string to bytes. Here's what I did, you can just put the entire thing inside send()
instead of creating a new variable.
What I did here is create a new variable called 'b'. Then I converted the 'e' variable from string to bytes, and I specified the encoding as 'utf-8'. This is useful because when my SSH server script decodes this data, it will use an 'utf-8' encoding by default. So, it's ideal to send data in the same encoding. Next, I just swap the str(e)
inside the send function with the variable 'b'.
The error vanishes and my code if fully functional now. I will need to add a decode()
method in my SSH server script but that's a requirement already so it's no problem. Here you go python, have your bites ;)