How To Find Location Using Ip Address In Python
Sometimes you'll need to know the location of an IP address, whether it's your ain or that of a site you're using.
One use-instance for this is when you want to send login information to users for your website.
In this article, we're going to encounter how you can find the location of an IP address using Python.
To achieve this goal, we'll be using two APIs mentioned below:
- ipify : This API will help united states of america know the IP accost from where the request is coming.
- ipapi : This API will help us fetch location information for a item IP address.
To interact with these APIs, we'll exist using the requests
library in Python. If you're new to APIs, make sure you check out this tutorial to larn 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 become!
Become Location Information
As nosotros discussed, we'll commencement fetch our IP address from the first API. Then we'll brand use of this IP address to fetch location information for this particular IP address. Then, we'll have two functions:
import requests def 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_data print(get_location())
In the above code, we take two functions – get_ip()
and get_location()
. Let'south discuss each of them separately.
get_ip()
function
Equally per the API documentation of ipify, we demand to make a Get asking 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 response
variable which is nothing just a sort of Python dictionary with one fundamental-value pair. Then we returned the value of the key ip
every bit response["ip"]
.
get_location()
part
As per the API documentation of ipapi, we need to make a Become request on https://ipapi.co/{ip}/{format}/
to go location data for a detail IP accost. {ip}
is replaced past 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 accost and then makes a GET request on the URL with the IP address. This API returns a JSON response that looks similar this:
{ "ip": "117.214.109.137", "version": "IPv4", "city": "Gaya", "region": "Bihar", "region_code": "BR", "state": "IN", "country_name": "India", "country_code": "IN", "country_code_iso3": "IND", "country_capital": "New Delhi", "country_tld": ".in", "continent_code": "Equally", "in_eu": imitation, "postal": "823002", "breadth": 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,every bit,bh,sabbatum,ks,ne,sd,kok,doi,mni,sit,sa,fr,lus,inc", "country_area": 3287590, "country_population": 1352617328, "asn": "AS9829", "org": "National Internet Backbone" }
We become a whole lot of information in the response. Yous can apply any works for you. For this tutorial, we'll but be using city
, region
and country
. That'due south why nosotros created a dictionary chosen location_data
and stored all the data inside it and returned the aforementioned.
At concluding, 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": "Bharat" }
Conclusion
In this commodity, nosotros learned how nosotros can interact with web services to get location data for a particular IP address.
Thanks for reading! For more such manufactures, checkout my blog, iRead.
Learn to lawmaking for free. freeCodeCamp'southward open source curriculum has helped more than than twoscore,000 people become jobs as developers. Go started
Source: https://www.freecodecamp.org/news/how-to-get-location-information-of-ip-address-using-python/
Posted by: richiesalmor1959.blogspot.com
0 Response to "How To Find Location Using Ip Address In Python"
Post a Comment