def test_geoip_raw_geolocation(self): loc = geoip.GeoLocation(GEO_TEST_IP) loc_raw = geoip.GeoLocation(GEO_TEST_IP, result=geoip.lookup(GEO_TEST_IP)) for field in geoip.DB_RESULT_FIELDS: self.assertEqual(getattr(loc, field), getattr(loc_raw, field)) self.assertIsInstance(loc.ip_address, ipaddress.IPv4Address) self.assertEqual(loc.ip_address, ipaddress.ip_address(GEO_TEST_IP))
def from_ip_address(cls, ip_address): ip_address = ipaddress.ip_address(ip_address) if ip_address.is_private: return result = geoip.lookup(ip_address) if result is None: return return cls(**result)
def from_ip_address(cls, ip_address): ip_address = ipaddress.ip_address(ip_address) if ip_address.is_private: return try: result = geoip.lookup(ip_address) except geoip2.errors.AddressNotFoundError: result = None if result is None: return return cls(**result)
def rpc_geoip_lookup(self, ip, lang=None): """ Look up an IP address in the servers GeoIP database. If the IP address can not be found in the database, None will be returned. :param str ip: The IP address to look up. :param str lang: The language to prefer for regional names. :return: The geographic information for the specified IP address. :rtype: dict """ try: result = geoip.lookup(ip, lang=lang) except geoip.AddressNotFoundError: result = None return result
def rpc_geoip_lookup_multi(self, ips, lang=None): """ Look up multiple IP addresses in the servers GeoIP database. Each IP address that can not be found in the database will have its result set to None. :param list ips: The list of IP addresses to look up. :param str lang: The language to prefer for regional names. :return: A dictionary containing the results keyed by the specified IP addresses. :rtype: dict """ results = {} for ip in ips: try: result = geoip.lookup(ip, lang=lang) except geoip.AddressNotFoundError: result = None results[ip] = result return results
def rest_api_geoip_lookup(handler, params): ip = handler.get_query('ip') assert ip return geoip.lookup(ip)
def test_geoip_lookup_ipv6(self): with self.assertRaises(TypeError): geoip.lookup('2607:f8b0:4002:c07::68')
def test_geoip_lookup_private(self): with self.assertRaises(RuntimeError): geoip.lookup('192.168.1.1')
def test_geoip_lookup(self): result = geoip.lookup(GEO_TEST_IP) self.assertIsInstance(result, dict) for field in geoip.DB_RESULT_FIELDS: self.assertIn(field, result)
def rest_api_geoip_lookup(handler, params): ip = handler.get_query("ip") if not ip: logger.error("the required 'ip' parameter is missing for geoip/lookup") raise errors.KingPhisherAPIError("the required 'ip' parameter is missing for geoip/lookup") return geoip.lookup(ip)