How to Geolocate IP addresses in Python

Learn how you can extract geographic information about IP addresses using the IPInfo service and its wrapper in Python.
  · 3 min read · Updated jan 2023 · Ethical Hacking · Application Programming Interfaces

IP geolocation for information gathering is a very common task in information security. It is used to gather information about the user accessing the system, such as the country, city, address, and maybe even the latitude and longitude.

In this tutorial, we are going to perform IP geolocation using Python. There are many ways to perform such a task, but the most common is using the IPinfo service.

If you want to follow along, you should go ahead and register for an account at IPinfo. It’s worth noting that the free version of the service is limited to 50,000 requests per month, so that’s more than enough for us. Once registered, you go to the dashboard and grab your access token.

To use ipinfo.io in Python, we need to install its wrapper first:

$ pip install ipinfo

Open up a new Python file named get_ip_info.py and add the following code:

import ipinfo
import sys
# get the ip address from the command line
try:
    ip_address = sys.argv[1]
except IndexError:
    ip_address = None
# access token for ipinfo.io
access_token = '<put_your_access_token_here>'
# create a client object with the access token
handler = ipinfo.getHandler(access_token)
# get the ip info
details = handler.getDetails(ip_address)
# print the ip info
for key, value in details.all.items():
    print(f"{key}: {value}")

Pretty straightforward, we create the handler with the access token, and then we use the getDetails() method to get the location of the IP address. Make sure you replace the access_token with the access token you find in your dashboard. You can click on Copy to Clipboard to copy your access token:

Copying the access token in the IPinfo dashboard

Let’s run it on an example:

$ python get_ip_info.py 142.93.95.0 
ip: 142.93.95.0
city: Santa Clara
region: California
country: US
loc: 37.3483,-121.9844
org: AS14061 DigitalOcean, LLC
postal: 95051
timezone: America/Los_Angeles
country_name: United States
latitude: 37.3483
longitude: -121.9844

If you do not pass any IP address, the script will use the IP address of the computer it is running on. This is useful if you want to run the script from a remote machine.

Excellent! You've now learned how to perform IP geolocation in Python using the IPinfo.io service. You can check the GitHub repo for the library for more information.

Learn also: How to Get Geolocation in Python.

Are you interested in learning more about ethical hacking and building tools to protect against cyber threats? Our Ethical Hacking with Python Ebook is the perfect resource for you! This comprehensive guide covers building tools in various topics, including information gathering, malware, and network packet manipulation. You'll learn how to build tools like a reverse shell, password cracking tools, and many more (More than 35 pentesting tools).

With step-by-step instructions and clear explanations, this EBook is perfect for both beginners and experienced professionals looking to expand their knowledge in the field of ethical hacking with Python. Don't miss out on this opportunity to become an expert in cyber security – get your copy today!

Happy coding ♥

View Full Code
Sharing is caring!



Read Also



Comment panel