osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Irritating bytearray behavior


On 16Sep2019 20:37, Ian Pilcher <arequipeno at gmail.com> wrote:
>   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?

Sure:

    msg[4:4+len(ip.packed)] = ip.packed

What you did was ask to replace all the bytes from 4 onward. What you 
should do is replace just the correct bytes.

Cheers,
Cameron Simpson <cs at cskk.id.au>