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 test_geoip_geo_interface(self): location = geoip.GeoLocation(GEO_TEST_IP) self.assertTrue(hasattr(location, '__geo_interface__')) interface = location.__geo_interface__ self.assertIsInstance(interface, dict) self.assertEqual(interface.get('type'), 'Point') coordinates = interface.get('coordinates') self.assertIsInstance(coordinates, tuple)
def geoip_lookup(self, ip): """ Look up the geographic location information for the specified IP address in the server's geoip database. :param ip: The IP address to lookup. :type ip: :py:class:`ipaddress.IPv4Address`, str :return: The geographic location information for the specified IP address. :rtype: :py:class:`~king_phisher.geoip.GeoLocation` """ result = self.cache_call('geoip/lookup', str(ip)) if result: result = geoip.GeoLocation(ip, result=result) return result
def geoip_lookup_multi(self, ips): """ Look up the geographic location information for the specified IP addresses in the server's geoip database. Because results are cached for optimal performance, IP addresses to be queried should be grouped and sorted in a way that is unlikely to change, i.e. by a timestamp. :param ips: The IP addresses to lookup. :type ips: list, set, tuple :return: The geographic location information for the specified IP address. :rtype: dict """ ips = [str(ip) for ip in ips] results = self.cache_call('geoip/lookup/multi', ips) for ip, data in results.items(): results[ip] = geoip.GeoLocation(ip, result=data) return results