Skip to content Skip to sidebar Skip to footer

Send Byte Sequence Over Python Socket

I am trying to write a very basic server (for experimenting purposes) that listens to incoming connections on a port and when a client connects send a custom byte sequence to the c

Solution 1:

The send method requires a "bytes-like" object, which it looks like you are trying to pass in but aren't using the bytes function properly.

If you have actual binary data, like in your example, you can just create the bytes object directly:

s.send(b'\x01\x02\x03')

If you have a string (which is not the same as bytes in Python), then you have to encode the string into bytes. This can be done with the bytes function but you have to tell it what encoding to use. Generally ASCII or UTF-8 is used.

s.send(bytes('Hello world', 'ascii'))
s.send(bytes('Hello world', 'utf-8'))

Post a Comment for "Send Byte Sequence Over Python Socket"