Skip to main content

Simple tool for getting geolocation information on given IP address from various geolocation databases.

Project description

Description

ip2geotools is a simple tool for getting geolocation information on given IP address from various geolocation databases. This package provides an API for several geolocation databases.

Installation

To install the ip2geotools module, type:

$ pip install ip2geotools

Basic usage

>>> from ip2geotools.databases.noncommercial import DbIpCity
>>> response = DbIpCity.get('147.229.2.90', api_key='free')
>>> response.ip_address
'147.229.2.90'
>>> response.city
'Brno (Brno střed)'
>>> response.region
'South Moravian'
>>> response.country
'CZ'
>>> response.latitude
49.1926824
>>> response.longitude
16.6182105
>>> response.to_json()
'{"ip_address": "147.229.2.90", "city": "Brno (Brno st\u0159ed)", "region": "South Moravian", "country": "CZ", "latitude": 49.1926824, "longitude": 16.6182105}'
>>> response.to_xml()
'<?xml version="1.0" encoding="UTF-8" ?><ip_location><ip_address>147.229.2.90</ip_address><city>Brno (Brno střed)</city><region>South Moravian</region><country>CZ</country><latitude>49.1926824</latitude><longitude>16.6182105</longitude></ip_location>'
>>> response.to_csv(',')
'147.229.2.90,Brno (Brno střed),South Moravian,CZ,49.1926824,16.6182105'

Command-line usage

When installed, you can invoke ip2geotools from the command-line:

ip2geotools [-h] -d {dbipcity,hostip,freegeoip,ipstack,maxmindgeolite2city,ip2location,dbipweb,maxmindgeoip2city,ip2locationweb,neustarweb,geobytescitydetails,skyhookcontextacceleratorip,ipinfo,eurek,ipdata}
                   [--api_key API_KEY] [--db_path DB_PATH] [-u USERNAME]
                   [-p PASSWORD] [-f {json,xml,csv-space,csv-tab,inline}] [-v]
                   IP_ADDRESS

Where:

  • ip2geotools: is the script when installed in your environment, in development you could use python -m ip2geotools instead

  • IP_ADDRESS: IP address to be checked

  • -h, --help: show help message and exit

  • -d {dbipcity,hostip,...,eurek}: geolocation database to be used (case insesitive)

  • --api_key API_KEY: API key for given geolocation database (if needed)

  • --db_path DB_PATH: path to geolocation database file (if needed)

  • -u USERNAME, --username USERNAME: username for accessing given geolocation database (if needed)

  • -p PASSWORD, --password PASSWORD: password for accessing given geolocation database (if needed)

  • -f {json,xml,csv-space,csv-tab,inline}, --format {json,xml,csv-space,csv-tab,inline}: output data format

  • -v, --version: show program’s version number and exit

Examples:

$ ip2geotools 147.229.2.90 -d dbipcity -f json
{"ip_address": "147.229.2.90", "city": "Brno (Brno st\u0159ed)", "region": "South Moravian", "country": "CZ", "latitude": 49.1926824, "longitude": 16.6182105}

Models

This module contains models for the data returned by geolocation databases and these models are also used for comparison of given and provided data.

ip2geotools.models.IpLocation

Model for storing location of given IP address.

Attributes:

  • ip_address: IP address

  • city: city where IP address is located

  • region: region where IP address is located

  • country: country where IP address is located (two letters country code)

  • latitude: latitude where IP address is located

  • longitude: longitude where IP address is located

Methods:

  • to_json: returns model data in JSON format

  • to_xml: returns model data in XML format (root element: ip_location)

  • to_csv: returns model data in CSV format separated by given delimiter

  • __str__: internal string representation of model, every single information on new line

Exceptions

This module provides special exceptions used when accessing data from third-party geolocation databases.

  • ip2geotools.errors.LocationError: a generic location error

  • ip2geotools.errors.IpAddressNotFoundError: the IP address was not found

  • ip2geotools.errors.PermissionRequiredError: problem with authentication or authorization of the request; check your permission for accessing the service

  • ip2geotools.errors.InvalidRequestError: invalid request

  • ip2geotools.errors.InvalidResponseError: invalid response

  • ip2geotools.errors.ServiceError: response from geolocation database is invalid (not accessible, etc.)

  • ip2geotools.errors.LimitExceededError: limits of geolocation database have been reached

Databases

Following classes access many different noncommercial and commercial geolocation databases using defined interface.

ip2geotools.databases.interfaces

  • IGeoIpDatabase: interface for unified access to the data provided by various geolocation databases

ip2geotools.databases.noncommercial

ip2geotools.databases.commercial

Requirements

This code requires Python 3.3+ and several other packages listed in requirements.txt.

Support

Please report all issues with this code using the GitHub issue tracker

License

ip2geotools is released under the MIT License. See the bundled LICENSE file for details.

Author

ip2geotools was written by Tomas Caha <tomas-net at seznam dot cz> for master's thesis at FEEC BUT 2018/2019.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ip2geotools-0.1.3.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ip2geotools-0.1.3-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file ip2geotools-0.1.3.tar.gz.

File metadata

  • Download URL: ip2geotools-0.1.3.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.4.3

File hashes

Hashes for ip2geotools-0.1.3.tar.gz
Algorithm Hash digest
SHA256 f417d666aa77c9413ac00b4ec84338b752e6d5d093e91d455d970e2393643490
MD5 585d556c9b3d79a471913a1cd62004f7
BLAKE2b-256 10277639f5312706047036a68f211b1e45b16096ec73e510ce7ade3add1955e2

See more details on using hashes here.

File details

Details for the file ip2geotools-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: ip2geotools-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.4.3

File hashes

Hashes for ip2geotools-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8cf2db4681dc652a148c15774531237f4c79169c20a80937b18b267b8620ad53
MD5 ca79d84de77b474d9507e925422bf176
BLAKE2b-256 117daf05d529e6127236f5d953f934b64aa8685506f2422c39470eeff8173932

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page