geolocate_ips

privex.helpers.geoip.geolocate_ips(*addrs, throw=False) → Generator[Tuple[str, Optional[privex.helpers.geoip.GeoIPResult]], None, None][source]

Same as geolocate_ip() but accepts multiple IP addresses, and returns the results as a generator.

Usage:

>>> for ip, g in geolocate_ips('185.130.44.5', '8.8.4.4', '2a07:e00::333'):
...     print(f"{ip:<20} -> {str(g.city):<15} {str(g.country):<15} ({g.as_number} {g.as_name})")
185.130.44.5         -> Stockholm       Sweden          (210083 Privex Inc.)
8.8.4.4              -> None            United States   (15169 Google LLC)
2a07:e00::333        -> Stockholm       Sweden          (210083 Privex Inc.)
>>> data = dict(geolocate_ips('185.130.44.5', '8.8.4.4', '2a07:e00::333'))
>>> data['8.8.4.4'].country
'United States'
>>> data['2a07:e00::333'].as_name
'Privex Inc.'
Parameters
  • addrs (IP_OR_STR) – One or more IPv4 or IPv6 addresses to geo-locate

  • throw (bool) – (Default: True) If True, will raise GeoIPAddressNotFound if an IP address isn’t found in the GeoIP database. If False, will simply return None if it’s not found.

Raises
  • GeoIPAddressNotFound – When throw is True and one of the addrs can’t be found in a GeoIP database.

  • ValueError – When throw is True and one of the addrs is not a valid IP address.

Return Tuple[str, Optional[GeoIPResult]] res

A generator which returns tuples containing the matching IP address, and the GeoIPResult object containing the GeoIP data for the IP - or None if throw is False, and the IP address wasn’t found in the database.