Python bindings for https://ipapi.co (IP Address Location) - Use with python / django / flask for IP address location lookup
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
See the ipapi API docs
pip install --upgrade ipapi
or Install from source with:
python setup.py install
Python 2.7+ or Python 3.4+
>>> import ipapi
>>> ipapi.location()
{
"ip": "8.8.8.8",
"city": "Mountain View",
"country": "US",
"timezone": "America/Los_Angeles",
...
}
import ipapi
ipapi.location(ip, key, output)
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
>>> 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"
}
>>> 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')
xml
format (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>'
>>> ipapi.location(output='ip')
'50.1.2.3'
>>> ipapi.location(ip='8.8.8.8', output='city')
'Mountain View'
>>> ipapi.location(ip='8.8.8.8', output='country_code')
'US'
>>> ipapi.location(ip='8.8.8.8', output='region_code')
'CA'
>>> ipapi.location(ip='8.8.8.8', output='in_eu')
'False'
>>> ipapi.location(ip='1.2.3.4', output='latlong')
'-27.473101,153.014046'
>>> ipapi.location(ip='1.2.3.4', output='postal')
'4101'
>>> ipapi.location(ip='1.2.3.4', output='timezone')
'Australia/Brisbane'
>>> ipapi.location(ip='1.2.3.4', output='currency')
'AUD'
>>> ipapi.location(ip='1.1.1.1', output='asn')
'AS13335'
>>> ipapi.location(ip='8.8.8.8', output='org')
'GOOGLE'
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.
$ 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"
}
$ python ipapi -i '8.8.8.8'
{
"ip": "8.8.8.8",
"city": "Mountain View",
"region": "California",
...
}
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>'
$ python ipapi -o ip
'50.1.2.3'
$ python ipapi -i '8.8.8.8' -o city
'Mountain View'