def query(ip_addr): """Returns informational data specific to this IP address.""" info = {} if ip_addr.version == 4: for cidr, record in _dict_items(IANA_INFO['IPv4']): if _within_bounds(ip_addr, cidr): info.setdefault('IPv4', []) info['IPv4'].append(record) if ip_addr.is_multicast(): for iprange, record in _dict_items(IANA_INFO['multicast']): if _within_bounds(ip_addr, iprange): info.setdefault('Multicast', []) info['Multicast'].append(record) elif ip_addr.version == 6: for cidr, record in _dict_items(IANA_INFO['IPv6']): if _within_bounds(ip_addr, cidr): info.setdefault('IPv6', []) info['IPv6'].append(record) for cidr, record in _dict_items(IANA_INFO['IPv6_unicast']): if _within_bounds(ip_addr, cidr): info.setdefault('IPv6_unicast', []) info['IPv6_unicast'].append(record) return info
def query(ip_addr): """ Returns informational data specific to this IP address. """ info = {} def within_bounds(ip, ip_range): # Boundary checking for multiple IP classes. if hasattr(ip_range, 'first'): # IP network or IP range. return ip in ip_range elif hasattr(ip_range, 'value'): # IP address. return ip == ip_range raise Exception('Unsupported IP range or address: %r!' % ip_range) if ip_addr.version == 4: for cidr, record in _dict_items(IANA_INFO['IPv4']): if within_bounds(ip_addr, cidr): info.setdefault('IPv4', []) info['IPv4'].append(record) if ip_addr.is_multicast(): for iprange, record in _dict_items(IANA_INFO['multicast']): if within_bounds(ip_addr, iprange): info.setdefault('Multicast', []) info['Multicast'].append(record) elif ip_addr.version == 6: for cidr, record in _dict_items(IANA_INFO['IPv6']): if within_bounds(ip_addr, cidr): info.setdefault('IPv6', []) info['IPv6'].append(record) return info
def test_compat_dict_operations(): d = {'a': 0, 'b': 1, 'c': 2} assert sorted(_dict_keys(d)) == ['a', 'b', 'c'] assert sorted(_dict_items(d)) == [('a', 0), ('b', 1), ('c', 2)]
def test_compat_dict_operations(): d = { 'a' : 0, 'b' : 1, 'c' : 2 } assert sorted(_dict_keys(d)) == ['a', 'b', 'c'] assert sorted(_dict_items(d)) == [('a', 0), ('b', 1), ('c', 2)]