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.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to