geolocate_ip

privex.helpers.geoip.geolocate_ip(addr: Union[str, ipaddress.IPv4Address, ipaddress.IPv6Address], throw=True) → Optional[privex.helpers.geoip.GeoIPResult][source]

Looks up the IPv4/IPv6 address addr against GeoIP2 City + ASN, and returns a GeoIPResult containing the GeoIP data.

Usage:

>>> g = geolocate_ip('2a07:e00::333')
>>> print(g.city, g.country, g.country_code, g.as_number, g.as_name, sep='      ')
Stockholm       Sweden  SE      210083  Privex Inc.
>>> g = geolocate_ip('8.8.4.4')
None    United States   US      15169   Google LLC
Parameters
  • addr (IP_OR_STR) – An IPv4 or IPv6 address 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 addr can’t be found in a GeoIP database.

  • ValueError – When addr is not a valid IP address.

Return Optional[GeoIPResult] res

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