Click here to code!

PyBites Module of the Week - ipaddress

Posted by Julian on Thu 23 March 2017 in Modules • 3 min read

While playing around with code for our post on generators we discovered the ipaddress module, part of the Standard Library. Such a handy little module!

What does it do?

The ipaddress module simplifies various IP address related tasks. I’m going to outline some of the handier abilities below.

It may seem a little redundant to have a module dedicated solely to IP address related tasks but believe me when I say it can save you a heap of time as an admin and programmer.

Create an IPv4 or IPv6 address

The ipaddress module allows you to automatically create an IPv4 or IPv6 address without having to specify the type of address.

Just call .ip_address on ipaddress to let the modules figure it out for you:

>>> ipaddress.ip_address('')
>>> ipaddress.ip_address('fe80:0:0:0:200:f8ff:fe21:67cf')

Super simple!

Defining your host interface

To describe your particular host interface on a network, you call .ip_interface. Note that this uses the now normal notation: ‘’.

>>> ipaddress.ip_interface('')

Checking your ipaddress object

Now, as Python rocks, we can then assign these values to an object/variable and play with them:

>>> #Is it IPv4 or IPv6?
>>> ip = ipaddress.ip_address(
>>> ip.version

What’s the netmask?

I hate trying to remember netmasks and how the differing notations match up. I tend to stick with the loveable IP format of whereas others I know love to use the hate-filled method. (There, I said it!).

Now I never have to plug my netmasks into a shady online calculator again:

>>> net0
>>> net0.netmask

Kaloo Kalay!

Defining and checking a network

You can also define an entire network as per the following. I’ll show you why this is awesome in a second:

>>> #Define the network first
>>> net0 = ipaddress.ip_network('')
>>> #Now check to see how many addresses are valid for this network
>>> net0.num_addresses
>>> 256

List out the IP addresses for any given network

This is hands down my favourite feature of the ipaddress module for two reasons:

  1. If it hasn’t been a standard Class C (/24) network, I’ve struggled to figure out the valid IP addresses for the network.

  2. Creating a list of IP addresses for a given subnet has been a tedious process of iteration.

Here’s how the ipaddress module shows me some love:

>>> net0 = ipaddress.ip_network('')
>>> for i in net0.hosts():
...     print(i)


Or we can pop net0.hosts() (a generator) into a list and manipulate it as we see fit:

>>> iplist = list(net0.hosts())
>>> f'Bob can take IP {iplist[57]}'
'Bob can take IP'

Playing nice with other modules

As you play around with the ipaddress module you’ll find that the objects won’t play nice with other modules until converted to strings or integers. As per Section on the official Python documentation.

>>> ip = ipaddress.ip_address('')
>>> str(ip)
>>> int(ip)


It may not be the most exciting module out there but as someone who deals with many servers on a daily basis and loves to script these interactions, the ipaddress module is invaluable to me.

Even if it’s just to convert netmask notation…

Keep Calm and Code in Python!

— Julian

See an error in this post? Please submit a pull request on Github.