Network IP range checking

IP range conversion

We need to be able to check for IP ranges

import ipaddress
0.2s
ip_range_iterator = ipaddress.summarize_address_range(
  ipaddress.IPv4Address('155.56.127.255'),
  ipaddress.IPv4Address("155.56.127.255")
)
0.2s

how long is this range? We have an iterator, so we need we need to make it a list to see what's inside it.

ip_range = [ip for ip in ip_range_iterator]
0.1s

This is a list of networks, and the /32 basically means this an single IP address, not a range full of entries. We can check this by looking at the first entry in the list.

network = ip_range[0]
0.2s

And then sanity checking with num_addresses.

network.num_addresses
0.2s

We represent IP ranges as actual numbers in the database, and we can see what number is it by converting the IP address into an integer with int.

int(network[0])
0.2s
int(ipaddress.IPv4Address('155.56.127.255'))
0.2s

We have an IP range that begins and ends with 155.56.127.255, which we now know 2604171263.

The IP range 159.69.31.189 is showing as between them. What does that work out to be?

int(ipaddress.IPv4Address('159.69.31.189'))
0.2s

OK, 2672107453 is definitely not between 2604171263 and 2604171263.

This looks like bug in our code that checks if an IP is within the IP we think it is.

Runtimes (1)