Irritating bytearray behavior
On 2019-09-17 02:37, Ian Pilcher wrote:
> I am using a bytearray to construct a very simple message, that will be
> sent across the network. The message should always be 20 bytes:
> 2 bytes - address family (AF_INET or AF_INET6) - network byte order
> 2 bytes - (padding)
> 4 or 16 bytes - IP address
> The size of the IP address is dependent on whether it is an IPv4 address
> (4 bytes) or an IPv6 address (16 bytes). In the IPv4 case, it should be
> followed by 12 bytes of padding, to keep the message size consistent.
> Na?vely, I thought that I could do this:
> ip = ipaddress.ip_address(unicode(addr))
> msg = bytearray(20)
> msg = socket.AF_INET if ip.version == 4 else socket.AF_INET6
> msg[4:] = ip.packed
> sock.sendto(msg, dest)
> This doesn't work in the IPv4 case, because the bytearray gets truncated
> to only 8 bytes (4 bytes plus the size of ip.packed).
> Is there a way to avoid this behavior copy the contents of ip.packed
> into the bytearray without changing its size?
The truncation you're seeing is perfectly normal. It's the same as for
msg[4 : 4 + len(ip.packed)] = ip.packed
Alternatively, you could build the message a bytestrings and then pad
with the .ljust method.