Addresses, ranges and interfaces

Addresses

IPv4 addresses

class IPv4Address(self, addr=None)

Bases: object

Encapsulate an IPv4 address.

IPv4Address implements rich comparison:

IPv4Address("192.168.0.1") != IPv4Address("8.8.8.8")
IPv4Address("192.168.0.1") > IPv4Address("8.8.8.8")

IPv4Address can be hashed:

print(hash(IPv4Address("192.168.0.1")))

To get the integer representation:

print(int(IPv4Address("192.168.0.1")))

IPv4 ranges can be be built from addresses:

range = IPv4Address("192.168.0.1") / 24
Parameters:addr (int or bytes or IPv4Address) -- make an IPv4 address from this object
is_broadcast()
Returns:bool -- True if the address is a broadcast address.
is_loopback()
Returns:bool -- True if the address is a loopback address.
is_multicast()
Returns:bool -- True if the address is a multicast address.
is_private()
Returns:bool -- True if the address is a private address.
is_unicast()
Returns:bool -- True if the address is a unicast address.

IPv6 addresses

class IPv6Address(self, object addr=None)

Bases: object

Encapsulate an IPv6 address.

Parameters:addr (bytes or IPv6Address) -- make an IPv6 address from this object
full_repr()
Returns:list of int -- the 6 bytes composing the address as a list of integers.
is_loopback()
Returns:bool -- True if the address is a loopback address.
is_multicast()
Returns:bool -- True if the address is a multicast address.

Hardware addresses

class HWAddress(self, object addr=None)

Bases: object

Represents the address of a network hardware address

Parameters:addr (bytes or HWAddress) -- make a hardware address from this object
full_repr()
Returns:list of int -- the 6 bytes composing the address as a list of integers.
is_broadcast()
Returns:bool -- True if the address is a broadcast address.
is_multicast()
Returns:bool -- True if the address is a muticast address.
is_unicast()
Returns:bool -- True if the address is a unicast address.

Ranges

IPv4 address ranges

class IPv4Range(first=None, last=None, only_hosts=False, mask=None)

Bases: object

Represents a range of IPv4 addresses.

To build a range from an address and a mask:

range = IPv4Address("192.168.0.0") / 16
range = IPv4Range(first="192.168.0.0", mask="255.255.0.0")
range = IPv4Range.from_mask("192.168.0.0", 24)

You can test if an address belongs to a range:

if address in range:
    print("OK")

You can also iterate over ranges:

for i, address in enumerate(range):
    print(i, address)
Parameters:
  • first (bytes or IPv4Address) -- first address in range
  • last (bytes or IPv4Address) -- last address in range
  • only_hosts (bool) -- indicates whether only host addresses should be accessed when iterating the range
  • mask (bytes or IPv4Address) -- range mask

Note

Provide last OR mask

from_mask(address, mask)

Construct a range from an address and a mask.

Parameters:
  • address (bytes or IPv4Address) -- address (ex: 192.168.1.0)
  • mask (bytes or IPv4Address) -- mask (ex: 255.255.255.0)
Returns:

range (IPv4Range) -- new IPv4 range

Note

class method

is_iterable()
Returns:bool -- True if the range is iterable.
size()
Returns:int -- the range's size (how many addresses in the range)
first

The first address in range (read-only property)

last

The last address in range (read-only property)

IPv6 address ranges

class IPv6Range

Bases: object

Represents a range of IPv6 addresses.

You can test if an address belongs to a range:

if address in range:
    print("OK")

You can also iterate over ranges:

for i, address in enumerate(range):
    print(i, address)
from_mask()

Construct an IPv6Range from an address and a mask

Parameters:
Returns:

range (IPv6Range) -- new IPv6 range

is_iterable()
Returns:bool -- True if the range is iterable.
size()
Returns:int -- the range's size (how many addresses in the range)
first

First adddress in range (read-only property)

last

Last address in range (read-only propperty)

Harware address ranges

class HWRange

Bases: object

Represents a range of hardware addresses.

You can test if an address belongs to a range:

if address in range:
    print("OK")

You can also iterate over ranges:

for i, address in enumerate(range):
    print(i, address)
from_mask()

Construct a HWRange from an address and a mask

Parameters:
  • address (bytes or HWAddress) -- base hardware address
  • mask (bytes or HWAddress) -- range mask
Returns:

range (HWRange) -- new harware range

is_iterable()
Returns:bool -- True if the range is iterable.
size()
Returns:int -- the range's size (how many addresses in the range)
first

First address in range (read-only property)

last

Last address in range (read-only property)

Network interfaces

class NetworkInterface(name=None, address=None)

Bases: object

Represent a network interface

NetworkInterface objects support equality:

if not NetworkInterface() != NetworkInterface.default():
    print('boo')

NetworkInterface objects can be hashed:

print(hash(NetworkInterface.default()))
Parameters:
  • name (bytes, optional) -- if name is present (ex: 'eth0'), returns this network interface
  • address (bytes or IPv4Address, optional) -- if address is present, returns the interface that would be used to send packets to this address

Note

Give only one parameter, name OR address.

class NI_addresses_tuple(address, netmask, broadcast, hardware)

Bases: tuple

_asdict()

Return a new OrderedDict which maps field names to their values

_replace(_self, **kwds)

Return a new NI_addresses_tuple object replacing specified fields with new values

address

Alias for field number 0

broadcast

Alias for field number 2

hardware

Alias for field number 3

netmask

Alias for field number 1

NetworkInterface.all()
Returns:all (list of NetworkInterface) -- a list of all network interfaces

Note

class method

NetworkInterface.default()
Returns:default (NetworkInterface) -- the default interface.

Note

class method

NetworkInterface.equals(other)
Parameters:other (object) -- any python object
Returns:bool -- Returns True if self equals other
NetworkInterface.is_loopback()
Returns:bool -- True if the interface is a loopback interface.
NetworkInterface.addresses

the IPv4 address, netmask, broadcast address and hardware addresss associated with this interface.

NetworkInterface.id

Returns the interface id (read-only property)

NetworkInterface.name

Returns the interface name (read-only property)