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 aGeoIPResult
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
) IfTrue
, will raiseGeoIPAddressNotFound
if an IP address isn’t found in the GeoIP database. IfFalse
, will simply returnNone
if it’s not found.
- Raises
GeoIPAddressNotFound – When
throw
isTrue
andaddr
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 - orNone
ifthrow
isFalse
, and the IP address wasn’t found in the database.