ipapi Python Library ~ ipapi.co by Kloudend, Inc.
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
- 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"
}
- 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')
- Find the location of an IP address in
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>'
- Find your external IP address
>>> ipapi.location(output='ip')
'50.1.2.3'
- Find the city from an IP address
>>> ipapi.location(ip='8.8.8.8', output='city')
'Mountain View'
- Find the country code from an IP address
>>> ipapi.location(ip='8.8.8.8', output='country_code')
'US'
- Find the region of an IP address
>>> ipapi.location(ip='8.8.8.8', output='region_code')
'CA'
- Find if an IP address is located in the European Union
>>> ipapi.location(ip='8.8.8.8', output='in_eu')
'False'
- Find the latitude and longitude of an IP address
>>> ipapi.location(ip='1.2.3.4', output='latlong')
'-27.473101,153.014046'
- Find the postal code of an IP address
>>> ipapi.location(ip='1.2.3.4', output='postal')
'4101'
- Find the timezone of an IP address
>>> ipapi.location(ip='1.2.3.4', output='timezone')
'Australia/Brisbane'
- Find the currency of an IP address
>>> ipapi.location(ip='1.2.3.4', output='currency')
'AUD'
- Find the ASN of an IP address
>>> ipapi.location(ip='1.1.1.1', output='asn')
'AS13335'
- Find the Organization of an IP address
>>> 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.
- 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"
}
- Get the geolocation of an IP address
$ python ipapi -i '8.8.8.8'
{
"ip": "8.8.8.8",
"city": "Mountain View",
"region": "California",
...
}
- 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>'
- Get your external IP address
$ python ipapi -o ip
'50.1.2.3'
- Get the city of an IP address
$ python ipapi -i '8.8.8.8' -o city
'Mountain View'