Ipapi Python Save

Python bindings for https://ipapi.co (IP Address Location) - Use with python / django / flask for IP address location lookup

Project README

ipapi Python Library ~ ipapi.co by Kloudend, Inc.

IP Address Location | IP Lookup | IP Geolocation API

The ipapi Python library provides convenient access to the IP address location service from applications written in the Python language. It makes it easy to harness the potential of the IP geolocation API.

Details on free IP lookup and ipapi pricing plans

Documentation

See the ipapi API docs

Installation

pip install --upgrade ipapi

or Install from source with:

python setup.py install

Requirements

Python 2.7+ or Python 3.4+

QuickStart

>>> import ipapi

>>> ipapi.location()
{
  "ip": "8.8.8.8",
  "city": "Mountain View",
  "country": "US",
  "timezone": "America/Los_Angeles",
  ...
}

Usage : As an IP Location library

import ipapi

ipapi.location(ip, key, output)

Options

Argument Description
ip IP Address (IPv4 or IPv6) that you wish to locate.
If omitted, it defaults to the your machine's IP
key API key (for paid plans).
Omit it or set key=None for usage under free IP Location tier.
output The desired output from the API.
For complete IP location object, valid values are json, csv, xml, yaml.
To retrieve a specific field (e.g. city, country etc. as text), valid values are [1].
If omitted or None, gets the entire location data as json

[1] Fields supported by the API : ip, city, region, region_code, country, country_code, country_code_iso3, country_capital, country_tld, country_name, continent_code, in_eu, postal, latitude, longitude, timezone, utc_offset, country_calling_code, currency, currency_name, languages, country_area, country_population, latlong, asn, org

Examples

  1. Find the location of your IP address
>>> ipapi.location()

The output would be a JSON object like this (assuming your IP is '50.1.2.3') :

{
    "ip": "50.1.2.3",
    "city": "Sacramento",
    "region": "California",
    "region_code": "CA",
    "country": "US",
    "country_code": "US",
    "country_code_iso3": "USA",
    "country_capital": "Washington",
    "country_tld": ".us",
    "country_name": "United States",
    "continent_code": "NA",
    "in_eu": false,
    "postal": "95817",
    "latitude": 38.548,
    "longitude": -121.4597,
    "timezone": "America/Los_Angeles",
    "utc_offset": "-0700",
    "country_calling_code": "+1",
    "currency": "USD",
    "currency_name": "Dollar",
    "languages": "en-US,es-US,haw,fr",
    "country_area": 9629091.0,
    "country_population": 310232863.0,
    "asn": "AS7065",
    "org": "SONOMA"
}
  1. Find the location of an IP address
>>> ipapi.location(ip='8.8.8.8')
{
    "ip": "8.8.8.8",
    "city": "Mountain View",
    "region": "California",
    ...
}

You can also use an IPv6 address e.g.

>>> ipapi.location(ip='2001:4860:1::1')
  1. Find the location of an IP address in xmlformat (other formats :json, csv,yaml)
>>> ipapi.location(ip='8.8.8.8', output='xml')
'<?xml version="1.0" encoding="utf-8"?>
  <root>
    <ip>8.8.8.8</ip>
    <city>Mountain View</city>
    ...
  </root>'
  1. Find your external IP address
>>> ipapi.location(output='ip')
'50.1.2.3'
  1. Find the city from an IP address
>>> ipapi.location(ip='8.8.8.8', output='city')
'Mountain View'
  1. Find the country code from an IP address
>>> ipapi.location(ip='8.8.8.8', output='country_code')
'US'
  1. Find the region of an IP address
>>> ipapi.location(ip='8.8.8.8', output='region_code')
'CA'
  1. Find if an IP address is located in the European Union
>>> ipapi.location(ip='8.8.8.8', output='in_eu')
'False'
  1. Find the latitude and longitude of an IP address
>>> ipapi.location(ip='1.2.3.4', output='latlong')
'-27.473101,153.014046'
  1. Find the postal code of an IP address
>>> ipapi.location(ip='1.2.3.4', output='postal')
'4101'
  1. Find the timezone of an IP address
>>> ipapi.location(ip='1.2.3.4', output='timezone')
'Australia/Brisbane'
  1. Find the currency of an IP address
>>> ipapi.location(ip='1.2.3.4', output='currency')
'AUD'
  1. Find the ASN of an IP address
>>> ipapi.location(ip='1.1.1.1', output='asn')
'AS13335'
  1. Find the Organization of an IP address
>>> ipapi.location(ip='8.8.8.8', output='org')
'GOOGLE'

Usage : As an IP Location command line utility

ln -s /<virtual-env-path>/lib/python3.8/site-packages/ipapi ipapi

$ python ipapi -i <IP Address> -k <API KEY> -o <Output Format>
OR
$ python ipapi --ip <IP Address> --key <API KEY> --output <Output Format>

where the options ip, key, output are defined above.

Examples

  1. Get your IP Geolocation
$ python ipapi

The output would be a JSON object like this (assuming your IP is 50.1.2.3) :

{
    "ip": "50.1.2.3",
    "city": "Sacramento",
    "region": "California",
    "region_code": "CA",
    "country": "US",
    "country_code": "US",
    "country_code_iso3": "USA",
    "country_capital": "Washington",
    "country_tld": ".us",
    "country_name": "United States",
    "continent_code": "NA",
    "in_eu": false,
    "postal": "95817",
    "latitude": 38.548,
    "longitude": -121.4597,
    "timezone": "America/Los_Angeles",
    "utc_offset": "-0700",
    "country_calling_code": "+1",
    "currency": "USD",
    "currency_name": "Dollar",
    "languages": "en-US,es-US,haw,fr",
    "country_area": 9629091.0,
    "country_population": 310232863.0,
    "asn": "AS7065",
    "org": "SONOMA"
}
  1. Get the geolocation of an IP address
$ python ipapi -i '8.8.8.8'
{
    "ip": "8.8.8.8",
    "city": "Mountain View",
    "region": "California",
    ...
}
  1. Get the location of an IP in xml format (other formats : json, csv, yaml)
$ python ipapi -i '8.8.8.8' -o xml
'<?xml version="1.0" encoding="utf-8"?>
  <root>
    <ip>8.8.8.8</ip>
    <city>Mountain View</city>
    ...
  </root>'
  1. Get your external IP address
$ python ipapi -o ip
'50.1.2.3'
  1. Get the city of an IP address
$ python ipapi -i '8.8.8.8' -o city
'Mountain View'
Open Source Agenda is not affiliated with "Ipapi Python" Project. README Source: ipapi-co/ipapi-python

Open Source Agenda Badge

Open Source Agenda Rating