Skip to content

IP2Location Python API

ooi18 edited this page Jul 17, 2023 · 5 revisions

IP2Location Class

class IP2Location(database_file_path, file_mode)

  Initiate the IP2Location class and load the IP2Location BIN database.

  Parameters:

    database_file_path (str) – (Required) The file path links to IP2Location BIN databases.

    file_mode (str) – (Optional) The file mode used to open the IP2Location BIN database. Default is File I/O.

get_all (ip_address)

  Retrieve geolocation information for an IP address.

  Parameters:

      ip_address (str) – (Required) The IP address (IPv4 or IPv6).

  Returns:

      Returns the geolocation information in dict. Refer below table for the fields avaliable in the dict.

  Returns type:

      dict.

  RETURN FIELDS

Field Name Description
country_short Two-character country code based on ISO 3166.
country_long Country name based on ISO 3166.
region Region or state name.
city City name.
isp Internet Service Provider or company's name.
latitude City latitude. Defaults to capital city latitude if city is unknown.
longitude City longitude. Defaults to capital city longitude if city is unknown.
domain Internet domain name associated with IP address range.
zipcode ZIP code or Postal code. 172 countries supported.
timezone UTC time zone (with DST supported).
netspeed Internet connection type.
idd_code The IDD prefix to call the city from another country.
area_code A varying length number assigned to geographic areas for calls between cities. 223 countries supported](https://www.ip2location.com/area-code-coverage).
weather_code The special code to identify the nearest weather observation station.
weather_name The name of the nearest weather observation station.
mcc Mobile Country Codes (MCC) as defined in ITU E.212 for use in identifying mobile stations in wireless telephone networks, particularly GSM and UMTS networks.
mnc Mobile Network Code (MNC) is used in combination with a Mobile Country Code(MCC) to uniquely identify a mobile phone operator or carrier.
mobile_brand Commercial brand associated with the mobile carrier. You may click mobile carrier coverage to view the coverage report.
elevation Average height of city above sea level in meters (m).
usage_type Usage type classification of ISP or company.
address_type IP address types as defined in Internet Protocol version 4 (IPv4) and Internet Protocol version 6 (IPv6).
category The domain category based on IAB Tech Lab Content Taxonomy.
district District or county name.
asn Autonomous system number (ASN). BIN databases.
as_name Autonomous system (AS) name.

IP2LocationIPTools Class

class IP2LocationIPTools()

  Initiate IP2LocationIPTools class.

is_ipv4(ip_address)

  Verify if a string is a valid IPv4 address.

  Parameters:

    ip_address (str) – (Required) IP address.

  Returns:

    Return True if the IP address is a valid IPv4 address or False if it isn’t a valid IPv4 address.

  Return type:

    boolean

is_ipv6(ip_address)

  Verify if a string is a valid IPv6 address

  Parameters:

    ip_address (str) – (Required) IP address.

  Returns:

    Return True if the IP address is a valid IPv6 address or False if it isn’t a valid IPv6 address.

  Return type:

    boolean

ipv4_to_decimal(ip_address)

  Translate IPv4 address from dotted-decimal address to decimal format.

  Parameters:

    ip_address (str) – (Required) IPv4 address.

  Returns:

    Return the decimal format of the IPv4 address.

  Return type:

    int

decimal_to_ipv4(ip_number)

  Translate IPv4 address from decimal number to dotted-decimal address.

  Parameters:

    ip_number (str) – (Required) Decimal format of the IPv4 address.

  Returns:

    Returns the dotted-decimal format of the IPv4 address.

  Return type:

    string

ipv6_to_decimal(ip_address)

  Translate IPv6 address from hexadecimal address to decimal format.

  Parameters:

    ip_address (str) – (Required) IPv6 address.

  Returns:

    Return the decimal format of the IPv6 address.

  Return type:

    int

decimal_to_ipv6(ip_number)

  Translate IPv6 address from decimal number into hexadecimal address.

  Parameters:

    ip_number (str) – (Required) Decimal format of the IPv6 address.

  Returns:

    Returns the hexadecimal format of the IPv6 address.

  Return type:

    string

ipv4_to_cidr(ip_from, ip_to)

  Convert IPv4 range into a list of IPv4 CIDR notation.

  Parameters:

    ip_from (str) – (Required) The starting IPv4 address in the range.

    ip_to (str) – (Required) The ending IPv4 address in the range.

  Returns:

    Returns the list of IPv4 CIDR notation.

  Return type:

    list

cidr_to_ipv4(cidr)

  Convert IPv4 CIDR notation into a list of IPv4 addresses.

  Parameters:

    cidr (str) – (Required) IPv4 CIDR notation.

  Returns:

    Returns an array of IPv4 addresses.

  Return type:

    dict

ipv6_to_cidr(ip_from, ip_to)

  Convert IPv6 range into a list of IPv6 CIDR notation.

  Parameters:

    ip_from (str) – (Required) The starting IPv6 address in the range.

    ip_to (str) – (Required) The ending IPv6 address in the range.

  Returns:

    Returns the list of IPv6 CIDR notation.

  Return type:

    list

cidr_to_ipv6(cidr)

  Convert IPv6 CIDR notation into a list of IPv6 addresses.

  Parameters:

    cidr (str) – (Required) IPv6 CIDR notation.

  Returns:

    Returns an array of IPv6 addresses.

  Return type:

    dict

compressed_ipv6(ip_address)

  Compress a IPv6 to shorten the length.

  Parameters:

    ip_address (str) – (Required) IPv6 address.

  Returns:

    Returns the compressed version of IPv6 address.

  Return type:

    str

expand_ipv6(ip_address)

  Expand a shorten IPv6 to full length.

  Parameters:

    ip_address (str) – (Required) IPv6 address.

  Returns:

    Returns the extended version of IPv6 address.

  Return type:

    str

Country Class

class Country(csv_file_path)

  Initiate Country class and load the IP2Location Country Information CSV file. This database is free for download at https://www.ip2location.com/free/country-information.

  Parameters:

    csv_file_path (str) – (Required) The file path links to IP2Location Country Information CSV file.

get_country_info(country_code)

  Provide a ISO 3166 country code to get the country information in array. Will return a full list of countries information if country code not provided.

  Parameters:

    country_code (str) – (Required) The ISO 3166 country code of a country.

  Returns:

    Returns the country information in dict. Refer below table for the fields avaliable in the dict.

  Return type:

    dict

  RETURN FIELDS

Field Name Description
country_code Two-character country code based on ISO 3166.
country_alpha3_code Three-character country code based on ISO 3166.
country_numeric_code Three-character country code based on ISO 3166.
capital Capital of the country.
country_demonym Demonym of the country.
total_area Total area in km{sup}2.
population Population of year 2014.
idd_code The IDD prefix to call the city from another country.
currency_code Currency code based on ISO 4217.
currency_name Currency name.
currency_symbol Currency symbol.
lang_code Language code based on ISO 639.
lang_name Language name.
cctld Country-Code Top-Level Domain.

Region Class

class Region(csv_file_path)

  Initiate Region class and load the IP2Location ISO 3166-2 Subdivision Code CSV file. This database is free for download at https://www.ip2location.com/free/iso3166-2

  Parameters:

    csv_file_path (str) – (Required) The file path links to IP2Location ISO 3166-2 Subdivision Code CSV file.

get_region_code(country_code, region_name)

  Provide a ISO 3166 country code and the region name to get ISO 3166-2 subdivision code for the region.

  Parameters:

    country_code (str) – (Required) Two-character country code based on ISO 3166.

    region_name (str) – (Required) Region or state name.

  Returns:

    Returns the ISO 3166-2 subdivision code of the region.

  Return type:

    str