2.6. lars.geoip - GeoIP Database Access

This module provides a common interface to the GeoIP database. Most users will only need to be aware of the init_database() function in this module, which is used to initialize the GeoIP database(s). All other functions should be ignored; instead, users should use the country, region, city, and coords attributes of the IPv4Address and IPv6Address classes.

2.6.1. Functions

lars.geoip.country_code_by_addr(address)[source]

Returns the country code associated with the specified address, or None if the address is not found in the GeoIP geographical database. You should use the country or country attributes instead of this function.

If the geographical database for the address type has not been initialized, the function raises a ValueError.

Parameters:address (str) – The address to lookup the country for
Returns str:The country code associated with the address
lars.geoip.city_by_addr(address)[source]

Returns the city associated with the address. You should use the city or city attributes instead of this function.

Given an address, this function returns the city associated with it. Note: this function will raise an exception if the GeoIP database loaded is above city level.

If the geographical database for the address type has not been initialized, the function raises a ValueError.

Parameters:address (str) – The address to lookup the city for
Returns str:The city associated with the address, or None
lars.geoip.region_by_addr(address)[source]

Returns the region (e.g. state) associated with the address. You should use the region or region attributes instead of this function.

Given an address, this function returns the region associated with it. In the case of the US, this is the state. In the case of other countries it may be a state, county, something GeoIP-specific or simply undefined. Note: this function will raise an exception if the GeoIP database loaded is country-level only.

If the geographical database for the address type has not been initialized, the function raises a ValueError.

Parameters:address (str) – The address to lookup the region for
Returns str:The region associated with the address, or None
lars.geoip.coords_by_addr(address)[source]

Returns the coordinates (long, lat) associated with the address. You should use the coords or coords attributes instead of this function.

Given an address, this function returns a tuple with the attributes longitude and latitude (in that order) representing the (very) approximate coordinates of the address on the globe. Note: this function will raise an exception if the GeoIP database loaded is above city level.

If the geographical database for the address type has not been initialized, the function raises a ValueError.

Parameters:address (str) – The address to locate
Returns str:The coordinates associated with the address, or None

2.6.2. Examples