Sunday, September 21, 2008

Converting Subnet to CIDR in Python

A nice recipe, here is the meat of an even faster version in the comments.

def calcDottedNetmask(mask):
bits = 0xffffffff ^ (1 << 32 - mask) - 1
return inet_ntoa(pack('>I', bits))


Which also led me to the Python netaddr project. Very cool!

No comments: