• Latest
  • Trending
How to Get Location Information of an IP Address Using Python

How to Get Location Information of an IP Address Using Python

March 10, 2022
Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
Instagram fined €405m over children’s data privacy

Instagram fined €405m over children’s data privacy

September 6, 2022
8 Most Common Causes of a Data Breach

5.7bn data entries found exposed on Chinese VPN

August 18, 2022
Fibre optic interconnection linking Cameroon and Congo now operational

Fibre optic interconnection linking Cameroon and Congo now operational

July 15, 2022
Ericsson and MTN Rwandacell Discuss their Long-Term Partnership

Ericsson and MTN Rwandacell Discuss their Long-Term Partnership

July 15, 2022
Airtel Africa Purchases $42M Worth of Additional Spectrum

Airtel Africa Purchases $42M Worth of Additional Spectrum

July 15, 2022
Huawei steps up drive for Kenyan talent

Huawei steps up drive for Kenyan talent

July 15, 2022
TSMC predicts Q3 revenue boost thanks to increased iPhone 13 demand

TSMC predicts Q3 revenue boost thanks to increased iPhone 13 demand

July 15, 2022
Facebook to allow up to five profiles tied to one account

Facebook to allow up to five profiles tied to one account

July 15, 2022
Top 10 apps built and managed in Ghana

Top 10 apps built and managed in Ghana

July 15, 2022
MTN Group to Host the 2nd Edition of the MoMo API Hackathon

MTN Group to Host the 2nd Edition of the MoMo API Hackathon

July 15, 2022
KIOXIA Introduce JEDEC XFM Removable Storage with PCIe/NVMe Spec

KIOXIA Introduce JEDEC XFM Removable Storage with PCIe/NVMe Spec

July 15, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Sunday, 5 February, 2023
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

How to Get Location Information of an IP Address Using Python

A tutorial on how to fetch location information of an IP address using Requests and Python.

by ITECHNEWS
March 10, 2022
in Data Science, Leading Stories
0 0
0
How to Get Location Information of an IP Address Using Python

We often require to know the location for an IP address or maybe even ours. One other use-case for this is when you want to send login information to users for your website. In this blog, we’re going to see how we can know the location of an IP address using Python.

Get your tools ready

To accomplish the goal, we’ll be using two APIs mentioned below:

YOU MAY ALSO LIKE

Inaugural AfCFTA Conference on Women and Youth in Trade

Instagram fined €405m over children’s data privacy

  1. ipify: This API will help us know the IP address from where the request is coming.
  2. ipapi: This API will help us fetch location information for a particular IP address.

To interact with these APIs, we’ll be using requests library in Python. If you’re new to APIs, make sure you check out this tutorial to learn about them.

You can install this library using the pip command like this:

$ pip install requests

Once the library is installed, we’re good to go!

Get Location Information

As we discussed, we’ll first fetch our IP address from the first API. Then we’ll make use of this IP address to fetch location information for this particular IP address. So, we’ll have two functions.

import requestsdef get_ip():
    response = requests.get('https://api64.ipify.org?format=json').json()
    return response["ip"]def get_location():
    ip_address = get_ip()
    response = requests.get(f'https://ipapi.co/{ip_address}/json/').json()
    location_data = {
        "ip": ip_address,
        "city": response.get("city"),
        "region": response.get("region"),
        "country": response.get("country_name")
    }
    return location_dataprint(get_location())

In the above code, we have two functions- get_ip() and get_location(). Let’s discuss each of them separately.

get_ip() function

As per the API documentation of ipify, we need to make a GET request on https://api.ipify.org?format=json to get a JSON response that looks like this:

{
  "ip": "117.214.109.137"
}

We store this response in a variable which is nothing but a sort of Python dictionary with one key-value pair. Hence we returned the value of the key ip as response["ip"].

get_location() function

As per the API documentation of ipapi, we need to make a GET request on https://ipapi.co/{ip}/{format}/ to get location information for a particular IP address. {ip} is replaced by the IP address and {format} can be replaced with any of these – json, jsonp, xml, csv, yaml. This function internally calls the get_ip() function to get the IP address and then makes a GET request on the URL with the IP address. This API returns JSON response that looks like this:

{
  "ip": "117.214.109.137",
  "version": "IPv4",
  "city": "Gaya",
  "region": "Bihar",
  "region_code": "BR",
  "country": "IN",
  "country_name": "India",
  "country_code": "IN",
  "country_code_iso3": "IND",
  "country_capital": "New Delhi",
  "country_tld": ".in",
  "continent_code": "AS",
  "in_eu": false,
  "postal": "823002",
  "latitude": 24.7935,
  "longitude": 85.012,
  "timezone": "Asia/Kolkata",
  "utc_offset": "+0530",
  "country_calling_code": "+91",
  "currency": "INR",
  "currency_name": "Rupee",
  "languages": "en-IN,hi,bn,te,mr,ta,ur,gu,kn,ml,or,pa,as,bh,sat,ks,ne,sd,kok,doi,mni,sit,sa,fr,lus,inc",
  "country_area": 3287590,
  "country_population": 1352617328,
  "asn": "AS9829",
  "org": "National Internet Backbone"
}

We get a whole lot of data in the response. You can use whatever works for you. For this tutorial, we’ll just be using city, region and country. That’s why we created a dictionary called location_data and stored all the data inside it and returned the same.

At last, we call the get_location() function and print the output. Our output will look like this:

{
  "ip": "117.214.109.137", 
  "city": "Gaya", 
  "region": "Bihar", 
  "country": "India"
}

Conclusion

In this blog, we learned how we can interact with web services to get location information for a particular IP address.

Source: Ashutosh Krishna
Tags: IP Address Using Python
ShareTweetShare
Plugin Install : Subscribe Push Notification need OneSignal plugin to be installed.

Search

No Result
View All Result

Recent News

Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
Instagram fined €405m over children’s data privacy

Instagram fined €405m over children’s data privacy

September 6, 2022
8 Most Common Causes of a Data Breach

5.7bn data entries found exposed on Chinese VPN

August 18, 2022

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
Instagram fined €405m over children’s data privacy

Instagram fined €405m over children’s data privacy

September 6, 2022

Recent News

  • Inaugural AfCFTA Conference on Women and Youth in Trade September 6, 2022
  • Instagram fined €405m over children’s data privacy September 6, 2022
  • 5.7bn data entries found exposed on Chinese VPN August 18, 2022
  • Fibre optic interconnection linking Cameroon and Congo now operational July 15, 2022
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© 2021-2022 iTechNewsOnline.Com - Powered by BackUPDataSystems

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© 2021-2022 iTechNewsOnline.Com - Powered by BackUPDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Go to mobile version