Introduction

A fast, lightweight IPv4/IPv6 manipulation library in Python.

This library is used to create/poke/manipulate IPv4 and IPv6 addresses and networks.

Keywords

Arguments

addresses

Documentation

Collapse a list of IP objects.

Example: collapse_address_list([IPv4('1.1.0.0/24'), IPv4('1.1.1.0/24')]) -> [IPv4('1.1.0.0/23')]

Args: addresses: A list of IPv4Network or IPv6Network objects.

Returns: A list of IPv4Network or IPv6Network objects depending on what we were passed.

Raises: TypeError: If passed a list of mixed version objects.

Arguments

addresses

Documentation

Collapse a list of IP objects.

Example: collapse_address_list([IPv4('1.1.0.0/24'), IPv4('1.1.1.0/24')]) -> [IPv4('1.1.0.0/23')]

Args: addresses: A list of IPv4Network or IPv6Network objects.

Returns: A list of IPv4Network or IPv6Network objects depending on what we were passed.

Raises: TypeError: If passed a list of mixed version objects.

Arguments

obj

Documentation

Return a key suitable for sorting between networks and addresses.

Address and Network objects are not sortable by default; they're fundamentally different so the expression

IPv4Address('1.1.1.1') <= IPv4Network('1.1.1.1/24')

doesn't make any sense. There are some times however, where you may wish to have ipaddr sort these for you anyway. If you need to do this, you can use this function as the key= argument to sorted().

Args: obj: either a Network or Address object. Returns: appropriate key.

Arguments

address version
= None

Documentation

Take an IP string/int and return an object of the correct type.

Args: address: A string or integer, the IP address. Either IPv4 or IPv6 addresses may be supplied; integers less than 2**32 will be considered to be IPv4 by default. version: An Integer, 4 or 6. If set, don't try to automatically determine what the IP address type is. important for things like IPAddress(1), which could be IPv4, '0.0.0.1', or IPv6, '::1'.

Returns: An IPv4Address or IPv6Address object.

Raises: ValueError: if the string passed isn't either a v4 or a v6 address.

Arguments

address version
= None
strict
= False

Documentation

Take an IP string/int and return an object of the correct type.

Args: address: A string or integer, the IP address. Either IPv4 or IPv6 addresses may be supplied; integers less than 2**32 will be considered to be IPv4 by default. version: An Integer, if set, don't try to automatically determine what the IP address type is. important for things like IPNetwork(1), which could be IPv4, '0.0.0.1/32', or IPv6, '::1/128'.

Returns: An IPv4Network or IPv6Network object.

Raises: ValueError: if the string passed isn't either a v4 or a v6 address. Or if a strict network was requested and a strict network wasn't given.

Arguments

first last

Documentation

Summarize a network range given the first and last IP addresses.

Example: >>> summarize_address_range(IPv4Address('1.1.1.0'), IPv4Address('1.1.1.130')) [IPv4Network('1.1.1.0/25'), IPv4Network('1.1.1.128/31'), IPv4Network('1.1.1.130/32')]

Args: first: the first IPv4Address or IPv6Address in the range. last: the last IPv4Address or IPv6Address in the range.

Returns: The address range collapsed to a list of IPv4Network's or IPv6Network's.

Raise: TypeError: If the first and last objects are not IP addresses. If the first and last objects are not the same version. ValueError: If the last object is not greater than the first. If the version is not 4 or 6.

Arguments

address

Documentation

The binary representation of this address.

Args: address: An integer representation of an IPv4 IP address.

Returns: The binary representation of this address.

Raises: ValueError: If the integer is too large to be an IPv4 IP address.

Arguments

address

Documentation

The binary representation of this address.

Args: address: An integer representation of an IPv6 IP address.

Returns: The binary representation of this address.

ipaddr

image/svg+xml