Irritating bytearray behavior

Cameron Simpson cs at cskk.id.au
Mon Sep 16 22:09:35 EDT 2019


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>



More information about the Python-list mailing list