IPv4/Ipv6 object to bytes and bytes to IPv4/Ipv6 object transform.

[plum.ipaddress] Module Reference

The plum.ipaddress module provides transforms for converting standard library ipaddress module objects into bytes and vice versa. This reference page demonstrates creating and using these transforms as well as provides API details.

The examples shown on this page require the following setup:

>>> from ipaddress import IPv4Address
>>> from plum.ipaddress import IPv4AddressX
>>> from plum.utilities import pack, unpack

IPv4AddressX

Basic Usage

The IPv4AddressX transform accepts the following possible argument variations:

byteorder:"big" (default) or "little"
name:transform name (for representations including dump format column)

For example:

>>> ipv4address = IPv4AddressX(byteorder="little")

Use the transform to specify a format when using the pack() and unpack() utility functions or when using other high level transforms.

>>> fmt = [ipv4address, ipv4address]
>>> unpack(fmt, b'\x01\x01\xa8\xc0\x02\x01\xa8\xc0')
[IPv4Address('192.168.1.1'), IPv4Address('192.168.1.2')]
>>>
>>> pack(IPv4Address('192.168.1.1'), fmt=ipv4address)
b'\x01\x01\xa8\xc0'

The transform also accepts integer values when packing:

>>> pack(3232235777, fmt=ipv4address)
b'\x01\x01\xa8\xc0'

Tip

Use sys.byteorder as the IPv4AddressX transform byteorder argument to get the same byte order as the architecture of the machine your script is running on.

API Reference

class plum.ipaddress.IPv4AddressX(byteorder: Union[Literal[little], Literal[big]] = 'big', name: str = 'IPv4Address')

IPv4Address to bytes and bytes to IPv4Address transform.

byteorder

Byte order (“little” or “big”).

nbytes

Transform format size in bytes.

pack(value: Any) → bytes

Pack value as formatted bytes.

Raises:PackError if type error, value error, etc.
pack_and_dump(value: Any) → Tuple[bytes, plum.dump.Dump]

Pack value as formatted bytes and produce bytes summary.

Raises:PackError if type error, value error, etc.
unpack(buffer: bytes) → Any

Unpack value from formatted bytes.

Raises:UnpackError if insufficient bytes, excess bytes, or value error
unpack_and_dump(buffer: bytes) → Tuple[Any, plum.dump.Dump]

Unpack value from bytes and produce packed bytes summary.

Raises:UnpackError if insufficient bytes, excess bytes, or value error