DEV Community

Cover image for IP Address Geolocation data with Python in under 30 Seconds
Reincoder
Reincoder

Posted on • Updated on

IP Address Geolocation data with Python in under 30 Seconds

post header: Getting Started with IP Address data with Python in under 30 seconds and in 2 steps

IPinfo’s IP Address API service that keeps it simple. Whether it is making requests to the API or using the official Python library you can get up and running in no time.

The API service allows you to get tons of data and insights from an IP address including -
🗺 Geolocation data
🌎 ISP, hosting and company related data
🔐 Privacy and security data like VPN, Tor and Proxy usage

The IPinfo’s API gets you what you need in no time. They are pretty generous with their free tier too - they provide up to 50,000 free geolocation requests per month. Even better, no credit card is required and you can signup with your Google or Github account in under few seconds.

So start the clock and let’s see if it is possible to get IP address data in under 30 seconds!

Signing up to IPinfo & getting your access token (<10 Seconds)

Sign up with IPinfo

  1. You can use your Google account or your GitHub account
  2. Go to your dashboard and copy your access token from the bottom of the page!

IPinfo Dashboard

Installing the Python Module (<15 Seconds)

Assuming you have Python and Pip installed properly -

  1. Install the IPinfo Python module using pip-

    pip install ipinfo

  2. Once installed you are ready to go!

  3. To use the Python Module -

    Import → initialize the handler → getting information from the IP address using the handler.

import ipinfo

# get your access token from your dashboard
access_token = '0df49bbbc5e4eb'

# initialize the handler
handler = ipinfo.getHandler(access_token)

# declare the IP address
ip_address = '216.239.36.21' 

details = handler.getDetails(ip_address)

print(details.all)
Enter fullscreen mode Exit fullscreen mode

If you can write as fast me (I doubt that 😎) you can even do all that in less than 20 seconds. Want me to prove it?

So what kind of information you can get from a mere IP Address? Lots of information actually.

Here is the full data output

{
  'ip': '216.239.36.21',
  'hostname': 'any-in-2415.1e100.net',
  'anycast': True,
  'city': 'Mountain View',
  'region': 'California',
  'country': 'US',
  'loc': '37.4056,-122.0775',
  'postal': '94043',
  'timezone': 'America/Los_Angeles',
  'asn': 
    {
      'asn': 'AS15169',
      'name': 'Google LLC',
      'domain': 'google.com',
      'route': '216.239.36.0/24',
      'type': 'business'
     },
   'company':
     {
       'name': 'Google LLC',
       'domain': 'google.com',
       'type': 'business'
     },
   'privacy':
     {
       'vpn': False, 
       'proxy': False,
       'tor': False,
       'relay': False,
       'hosting': False,
       'service': ''
     }, 
  'country_name': 'United States',
  'latitude': '37.4056',
  'longitude': '-122.0775'
}
Enter fullscreen mode Exit fullscreen mode

You can also access individual data by calling specific functions

>> details.city
'Mountain View'
>> details.country
'US'
>> details.asn
{'asn': 'AS15169',
 'name': 'Google LLC',
 'domain': 'google.com',
 'route': '216.239.36.0/24',
 'type': 'business'}
>> details.asn
'37.4056,-122.0775'
Enter fullscreen mode Exit fullscreen mode

Now, if you want to get even faster you could try out our brand-spanking-new IPinfo CLI. It lets you skip all that Python stuff and get all the information from your terminal.


Want to reach out to me? Hit me up on Twitter at @anyfactor

Top comments (1)

Collapse
 
perfecto25 profile image
mrx

this is excellent thank you, been looking to replicate functionality of this site, whatismyipaddress.com

via API for mass query

works perfectly