Functional schmunctional...

Scott David Daniels Scott.Daniels at Acm.Org
Tue Feb 10 16:19:57 EST 2009


For expressiveness, try something like:

def ip2in(dotted_ip_addr):
     result = 0
     assert dotted_ip_addr.count('.') in (3, 7)
     for chunk in dotted_ip_addr.split('.'):
         result = (result << 8) + int(chunk)
     return result

def inet2ip(ip_number):
     assert 0 < ip_number < 1 << 48
     bytes = []
     while ip_number:
         bytes.append(ip_number & 255)
         ip_number >>= 8
     assert len(bytes) in (4, 6)
     return '.'.join(str(n) for n in reversed(bytes))

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list