IP Address Geolocation data with Python in under 30 Seconds
Tags: python, webdev, security, ipaddress
Published: 2022-08-24 09:44:00

![post header: Getting Started with IP Address data with Python in under 30 seconds and in 2 steps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w04c19gpcmm77hfuaoov.png)
  

[IPinfo’s IP Address API](https://ipinfo.io/) service that keeps it simple. Whether it is making requests to the [API](https://ipinfo.io/developers) or using the [official Python library](https://github.com/ipinfo/python) 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](https://ipinfo.io/signup)

1. You can use your Google account or your GitHub account
2. [Go to your dashboard](https://ipinfo.io/account/home) and copy your access token from the bottom of the page!

![IPinfo Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9gvp4timi5bakq75h5h6.png)
 

# Installing the Python Module (<15 Seconds)

Assuming you have Python and Pip installed properly - 

1. Install the [IPinfo Python module](https://github.com/ipinfo/python) 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.
    

```python
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)
```

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?](https://twitter.com/anyfactor/status/1562375803941560320)


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

Here is the full data output

```python
{
  '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'
}
```

You can also access individual data by calling specific functions

```python
>> 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'
```

Now, if you want to get even faster you could try out our brand-spanking-new [IPinfo CLI](https://github.com/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](https://twitter.com/anyfactor)