0

i saw that if i want to send in bytes something, i have to write s.send(b'your text'). That is good so far. But what happens if 'your text' is stored in a variable (like a = 'your text') and i want to send the variable a in bytes? I assumes and tested:

s.send(b 'a') #does not work,it will send only 'a' as byte
s.send(b(a)) #does not work, error 'b is not defined'
1

1 Answer 1

1

If you are using python3, you probably want the bytes function (which will in turn direct you to the docs of the bytesarray function for the detail).

You need to know the encoding of your your text in order to convert it to bytes. From the docs:

If [the source] is a string, you must also give the encoding (and optionally, errors) parameters; bytearray() then converts the string to bytes using str.encode().

So you will want something like s.send(bytes(a, 'utf-8')).

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.