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[1] = 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?

TIA

--
========================================================================
Ian Pilcher                                         arequip...@gmail.com
-------- "I grew up before Mark Zuckerberg invented friendship" --------
========================================================================

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to