Converts an ip to a hex value that can be used with a hex bit mask

def ip2hex(ip):
    '''
    Converts an ip to a hex value that can be used with a hex bit mask
    '''
    parts = ip.split(".")
    if len(parts) != 4: return None
    ipv = 0
    for part in parts:
        try:
            p = int(part)
            if p < 0 or p > 255: return None
            ipv = (ipv << 8) + p
        except:
            return None
    return ipv