SDKs overview

Hirak IP to Country primarily provides access to its IP geolocation data through downloadable databases rather than traditional SDKs that wrap a real-time API. This model requires developers to integrate the database files directly into their applications. This section outlines the common approaches for integrating Hirak IP to Country data, covering both official guidance for database parsing and community-contributed libraries that facilitate this process.

The core offering, such as the IP to Country Database, is distributed as a file (e.g., CSV, SQL, BIN format) that applications must process locally to perform lookups. This differs from typical SDK usage where a library makes HTTP requests to an external service. Developers leveraging Hirak IP to Country are responsible for storing, querying, and updating the database within their own infrastructure. The developer documentation for CountryIPBlocks provides details on parsing these database formats.

Official SDKs by language

Hirak IP to Country does not offer traditional, language-specific SDKs that encapsulate API calls, as their primary delivery method is via downloadable databases. Instead, their official guidance focuses on the methods for integrating and querying these database files. The official resources provide sample data and documentation to assist developers in building their own parsers and lookup functions across various programming languages. This table reflects the common integration points and the maturity of the documentation for each.

Language/Environment Integration Method Official Guidance Maturity
SQL (MySQL, PostgreSQL) Importing .sql database files Database schema and import instructions Stable
CSV/Text File Parsing Reading and parsing .csv or .txt files File format specifications and examples Stable
Custom Binary (BIN) Parsing proprietary .bin files Binary format documentation Stable
PHP Database interaction via standard database libraries PHP integration examples Supported
Python Database interaction via standard database libraries Python integration examples Supported

Installation

Installation for Hirak IP to Country involves downloading the chosen database format and integrating it into your application or database system. There are no package manager installations (e.g., pip install, npm install) in the traditional sense for an SDK, as the core product is a data file.

Database Download

  1. Purchase: Acquire an IP to Country Database license from the Hirak IP to Country website.
  2. Download: Access the customer portal to download the database file in your preferred format (e.g., SQL, CSV, BIN).

Integration Steps by Format

SQL Database (e.g., MySQL, PostgreSQL)

If you've downloaded an SQL dump, you typically import it into your existing relational database management system (RDBMS).

# Example for MySQL
mysql -u your_username -p your_database_name < ip_to_country.sql

# Example for PostgreSQL
psql -U your_username -d your_database_name -f ip_to_country.sql

After import, you can query the database table directly from your application using standard database connectors (e.g., ODBC drivers or language-specific ORMs).

CSV or Text File

For CSV or plain text files, you will need to implement a parser in your application's programming language. This can be done by loading the data into memory, using a specialized data structure, or querying it directly from the file system, though the latter can be less performant for large datasets.

# Example Python pseudocode for CSV parsing
import csv

def load_ip_data(filepath):
    ip_data = {}
    with open(filepath, 'r') as f:
        reader = csv.reader(f)
        for row in reader:
            # Assuming row[0] is IP range start, row[1] is IP range end, row[2] is country code
            ip_data[(row[0], row[1])] = row[2]
    return ip_data

# Or, a more robust solution might involve a binary search tree or a database like SQLite

Binary (BIN) File

Integrating the binary file format typically involves specialized parsing logic provided or outlined in the Hirak IP to Country developer documentation. This often requires understanding the byte structure of the file to efficiently perform IP lookups.

Quickstart example

This quickstart demonstrates a basic approach to performing an IP to Country lookup using a downloaded SQL database integrated with a Python application. This assumes the ip_to_country.sql file has been imported into a MySQL database named ip_geolocation_db and a table named ip_country_blocks exists with columns like ip_start, ip_end, and country_code.

import mysql.connector
import ipaddress # For IP address manipulation

def get_country_from_ip(ip_address_str):
    # Convert IP address string to integer for database comparison
    ip_int = int(ipaddress.IPv4Address(ip_address_str))

    try:
        # Establish database connection
        cnx = mysql.connector.connect(
            host="localhost",
            user="your_db_user",
            password="your_db_password",
            database="ip_geolocation_db"
        )
        cursor = cnx.cursor()

        # Query to find the country for the given IP
        # This query assumes ip_start and ip_end are stored as integers
        query = (
            "SELECT country_code FROM ip_country_blocks "
            "WHERE %s BETWEEN ip_start AND ip_end LIMIT 1"
        )
        
        cursor.execute(query, (ip_int,))
        result = cursor.fetchone()

        if result:
            return result[0]
        else:
            return "Unknown"

    except mysql.connector.Error as err:
        print(f"Database error: {err}")
        return None
    finally:
        if 'cnx' in locals() and cnx.is_connected():
            cursor.close()
            cnx.close()

# Example usage:
ip_to_lookup = "8.8.8.8" # Google DNS
country = get_country_from_ip(ip_to_lookup)
print(f"The IP address {ip_to_lookup} is in: {country}")

ip_to_lookup_2 = "192.168.1.1" # Private IP
country_2 = get_country_from_ip(ip_to_lookup_2)
print(f"The IP address {ip_to_lookup_2} is in: {country_2}")

ip_to_lookup_3 = "203.0.113.45" # Example public IP
country_3 = get_country_from_ip(ip_to_lookup_3)
print(f"The IP address {ip_to_lookup_3} is in: {country_3}")

This example demonstrates how to convert an IP address string to an integer for efficient database comparison and then execute a SQL query to find the corresponding country code. The use of ipaddress.IPv4Address is standard for handling IP addresses in Python, as described in the Python ipaddress module documentation.

Community libraries

Given Hirak IP to Country's database-centric delivery model, community efforts often focus on creating wrappers or helper libraries to simplify the process of loading and querying the downloaded data. These libraries are not officially endorsed but can provide convenience for specific programming environments.

  • Python IP Lookup Libraries: Community-contributed Python libraries sometimes emerge that can parse various IP geolocation database formats, including CSV or custom binary files. Developers often adapt general-purpose IP lookup libraries to work with the specific schema of Hirak IP to Country's data.
  • PHP IP Geolocation Packages: Similar to Python, the PHP ecosystem has packages designed for IP lookup by parsing large data files. These might require minor modifications to align with the Hirak IP to Country database structure.
  • Node.js IP Utilities: For Node.js environments, community modules might offer ways to load flat-file databases into memory or into local NoSQL databases for quick lookups.

When using community libraries, it is important to verify their compatibility with the specific database format purchased from Hirak IP to Country and to review their licensing and maintenance status. These libraries often handle tasks such as efficient IP to integer conversion, binary search implementation on sorted IP ranges, and caching mechanisms to improve lookup performance.