Get Started With IP2Location Python Module

Dependencies

This library requires IP2Location BIN database to function. You may download the BIN database at

Note

An outdated BIN database was provided in the databases folder for your testing. You are recommended to visit the above links to download the latest BIN database.


Requirements

  1. Python 3.5 and above.


Installation

Manual Installation

  1. Unzip the package.

  2. Execute python setup.py build

  3. Execute python setup.py install

Installation using PyPI.

pip install IP2Location

							
						

Installation in Arch Linux

For Arch Linux user, you can install the module using the following command:

git clone https://aur.archlinux.org/ip2location-python.git && cd ip2location-io-python
makepkg -si

							
						

Sample Codes

Query geolocation information from BIN database

You can query the geolocation information from the IP2Location BIN database as below:

import os
import IP2Location

"""
    Cache the database into memory to accelerate lookup speed.
    WARNING: Please make sure your system have sufficient RAM to use this feature.
"""
# database = IP2Location.IP2Location(os.path.join("data", "IPV6-COUNTRY.BIN"), "SHARED_MEMORY")

# Default file I/O lookup
database = IP2Location.IP2Location(os.path.join("data", "IPV6-COUNTRY.BIN"))

rec = database.get_all("19.5.10.1")

print("Country Code          : " + rec.country_short)
print("Country Name          : " + rec.country_long)
print("Region Name           : " + rec.region)
print("City Name             : " + rec.city)
print("ISP Name              : " + rec.isp)
print("Latitude              : " + rec.latitude)
print("Longitude             : " + rec.longitude)
print("Domain Name           : " + rec.domain)
print("ZIP Code              : " + rec.zipcode)
print("Time Zone             : " + rec.timezone)
print("Net Speed             : " + rec.netspeed)
print("Area Code             : " + rec.idd_code)
print("IDD Code              : " + rec.area_code)
print("Weather Station Code  : " + rec.weather_code)
print("Weather Station Name  : " + rec.weather_name)
print("MCC                   : " + rec.mcc)
print("MNC                   : " + rec.mnc)
print("Mobile Carrier        : " + rec.mobile_brand)
print("Elevation             : " + rec.elevation)
print("Usage Type            : " + rec.usage_type)
print("Address Type          : " + rec.address_type)
print("Category              : " + rec.category)
print("District              : " + rec.district)
print("ASN                   : " + rec.asn)
print("AS                    : " + rec.as_name)
print("\nYou may download the DB26 sample BIN at http://www.ip2location.com/downloads/sample6.bin.db26.zip for full data display.")

							
						

Processing IP address using IP Tools class

You can manupulate IP address, IP number and CIDR as below:

import IP2Location
ipTools = IP2Location.IP2LocationIPTools()
print(str(ipTools.is_ipv4("8.8.8.8")))
print(str(ipTools.is_ipv6("2001:4860:4860::8888")))
print(ipTools.ipv4_to_decimal("8.8.8.8"))
print(ipTools.decimal_to_ipv4(134744072))
print(ipTools.ipv6_to_decimal("2001:4860:4860::8888"))
print(ipTools.decimal_to_ipv6(42541956123769884636017138956568135816))
print(ipTools.ipv4_to_cidr("8.0.0.0", "8.255.255.255"))
print(ipTools.cidr_to_ipv4("8.0.0.0/8"))
print(ipTools.ipv6_to_cidr("2002:0000:0000:1234:abcd:ffff:c0a8:0000", "2002:0000:0000:1234:ffff:ffff:ffff:ffff"))
print(ipTools.cidr_to_ipv6("2002::1234:abcd:ffff:c0a8:101/64"))
print(ipTools.compressed_ipv6("2002:0000:0000:1234:FFFF:FFFF:FFFF:FFFF"))
print(ipTools.expand_ipv6("2002::1234:FFFF:FFFF:FFFF:FFFF"))

							
						

List down country information

You can query country information for a country from IP2Location Country Information CSV file as below:

import os
import IP2Location

# List country information
country = IP2Location.Country(os.path.join("data", "IP2LOCATION-COUNTRY-INFORMATION-BASIC.CSV"))
print(country.get_country_info("US"))

							
						

List down region information

You can get the region code by country code and region name from IP2Location ISO 3166-2 Subdivision Code CSV file as below:

import os
import IP2Location

# Get region code by country code and region
region = IP2Location.Region(os.path.join("data", "IP2LOCATION-ISO3166-2.CSV")
print(region.get_region_code("US", "California"))

							
						

IP2Location Sample Database (BIN)

Sample Packages - BIN File IPv4 IPv6
IP2Location DB1 Download (1,001.91 kB) Download (1.04 MB)
IP2Location DB2 Download (24.58 MB) Download (25.22 MB)
IP2Location DB3 Download (21.21 MB) Download (21.32 MB)
IP2Location DB4 Download (43.89 MB) Download (44.04 MB)
IP2Location DB5 Download (31.89 MB) Download (32.05 MB)
IP2Location DB6 Download (54.08 MB) Download (54.26 MB)
IP2Location DB7 Download (48.70 MB) Download (48.88 MB)
IP2Location DB8 Download (59.17 MB) Download (59.37 MB)
IP2Location DB9 Download (36.78 MB) Download (36.95 MB)
IP2Location DB10 Download (64.62 MB) Download (64.89 MB)
IP2Location DB11 Download (37.51 MB) Download (37.69 MB)
IP2Location DB12 Download (65.32 MB) Download (65.55 MB)
IP2Location DB13 Download (35.03 MB) Download (35.21 MB)
IP2Location DB14 Download (66.55 MB) Download (66.80 MB)
IP2Location DB15 Download (40.72 MB) Download (40.96 MB)
IP2Location DB16 Download (70.89 MB) Download (71.18 MB)
IP2Location DB17 Download (42.47 MB) Download (42.69 MB)
IP2Location DB18 Download (78.33 MB) Download (78.61 MB)
IP2Location DB19 Download (61.85 MB) Download (62.09 MB)
IP2Location DB20 Download (80.79 MB) Download (81.12 MB)
IP2Location DB21 Download (44.39 MB) Download (44.56 MB)
IP2Location DB22 Download (84.78 MB) Download (85.22 MB)
IP2Location DB23 Download (62.33 MB) Download (62.51 MB)
IP2Location DB24 Download (85.90 MB) Download (86.40 MB)
IP2Location DB25 Download (87.92 MB) Download (88.33 MB)
IP2Location DB26 Download (94.51 MB) Download (95.09 MB)

Articles & Tutorials