Ipcalc

This module allows you to perform IP subnet calculations, there is support for both IPv4 and IPv6 CIDR notation.

Example Usage

>>> import ipcalc
>>> for x in ipcalc.Network('172.16.42.0/30'):
...     print str(x)
...
172.16.42.0
172.16.42.1
172.16.42.2
172.16.42.3
>>> subnet = ipcalc.Network('2001:beef:babe::/48')
>>> print str(subnet.network())
2001:beef:babe:0000:0000:0000:0000:0000
>>> print str(subnet.netmask())
ffff:ffff:ffff:0000:0000:0000:0000:0000
>>> '192.168.42.23' in Network('192.168.42.0/24')
True
>>> long(IP('fe80::213:ceff:fee8:c937'))
338288524927261089654168587652869703991L

Bugs/Features

You can issue a ticket in GitHub: https://github.com/tehmaze/ipcalc/issues

Download

Get your copy of ipcalc from pypi: http://pypi.python.org/pypi/ipcalc

ipcalc IP subnet calculator

Note

BSD License

About

This module allows you to perform network calculations.

Changelog

Date Changes
2009-04-11 Added docstrings.
2009-03-23 Added IPv4 short-hand form support, thanks to VeXocide.
2007-10-26 Added IPv6 support, as well as a lot of other functions, refactored the calculations.
2007-10-25 Initial writeup, because I could not find any other workable implementation.

Todo

Todo:
  • add CLI parser

Thanks

I wish to thank the following people for their input:
  • Bastiaan (trbs)
  • Peter van Dijk (Habbie)
  • Hans van Kranenburg (Knorrie)
  • Jeroen Habraken (VeXocide)
  • Torbjörn Lönnemark
class ipcalc.IP(ip, mask=None, version=0)

Represents a single IP address.

Parameters:ip (IP or str or long or int) – the ip address
>>> localhost = IP("127.0.0.1")
>>> print localhost
127.0.0.1
>>> localhost6 = IP("::1")
>>> print localhost6
0000:0000:0000:0000:0000:0000:0000:0001
bin()

Full-length binary representation of the IP address.

>>> ip = IP("127.0.0.1")
>>> print ip.bin()
01111111000000000000000000000001
clone()

Return a new <IP> object with a copy of this one.

>>> ip = IP('127.0.0.1')
>>> ip.clone()
<ipcalc.IP object at 0xb7d4d18c>
hex()

Full-length hexadecimal representation of the IP address.

>>> ip = IP("127.0.0.1")
>>> print ip.hex()
7f000001
info()

Show IANA allocation information for the current IP address.

>>> ip = IP("127.0.0.1")
>>> print ip.info()
CLASS A
to_ipv4()

Convert (an IPv6) IP address to an IPv4 address, if possible. Only works for IPv4-compat (::/96) and 6-to-4 (2002::/16) addresses.

>>> ip = IP('2002:c000:022a::')
>>> print ip.to_ipv4()
192.0.2.42
to_ipv6(type='6-to-4')

Convert (an IPv4) IP address to an IPv6 address.

>>> ip = IP('192.0.2.42')
>>> print ip.to_ipv6()
2002:c000:022a:0000:0000:0000:0000:0000
to_tuple()

Used for comparisons.

version()

IP version.

>>> ip = IP("127.0.0.1")
>>> print ip.version()
4
class ipcalc.Network(ip, mask=None, version=0)

Network slice calculations.

Parameters:
  • ip (IP or str or long or int) – network address
  • mask (int or str) – netmask
>>> localnet = Network('127.0.0.1/8')
>>> print localnet
127.0.0.1
broadcast()

Broadcast address.

>>> localnet = Network('127.0.0.1/8')
>>> print localnet.broadcast()
127.255.255.255
has_key(ip)

Check if the given ip is part of the network.

Parameters:ip (IP or str or long or int) – the ip address
>>> net = Network('192.0.2.0/24')
>>> net.has_key('192.168.2.0')
False
>>> net.has_key('192.0.2.42')
True
host_first()

First available host in this subnet.

host_last()

Last available host in this subnet.

in_network(other)

Check if the given IP address is within this network.

netmask()

Network netmask derived from subnet size.

>>> localnet = Network('127.0.0.1/8')
>>> print localnet.netmask()
255.0.0.0
network()

Network address.

>>> localnet = Network('127.128.99.3/8')
>>> print localnet.network()
127.0.0.0
size()

Number of ip’s within the network.

>>> net = Network('192.0.2.0/24')
>>> print net.size()
256

Indices and tables

Table Of Contents

This Page