The socket
module provides inet_aton
, which will convert your addresses to bitstrings. You can then convert them to integers using struct.unpack
, mask using &
, and use integer comparison:
from socket import inet_aton
from struct import unpack
def atol(a):
return unpack(">L", inet_aton(a))[0]
addr = atol("30.44.230.0")
mask = atol("255.255.0.0")
lo = atol("32.44.230.0")
hi = atol("32.44.230.255")
prefix = addr & mask
print lo <= prefix <= hi