Python Ip to Location Details

python Location Tracking

Posted by indranil on Jan. 4, 2021, 10:15 p.m.

Python Ip To Location Trace Function

This package provides an API for the GeoIP2 web services and databases. The API also works with MaxMind’s free GeoLite2 databases.

Installation

To install the geoip2 module, type:

#pip install geoip2 //On Terminal

If you are not able to use pip, you may also use easy_install
from the source directory:

#easy_install

IP Geolocation Usage

IP geolocation is inherently imprecise. Locations are often near the center of the population. Any location provided by a GeoIP2 database or web service should not be used to identify a particular address or household.

Usage

To use this API, you first create either a web service object with your MaxMind account_id and license_key or a database reader object with the path to your database file. After doing this, you may call the method corresponding to request type (e.g., city or country), passing it the IP address you want to look up.

If the request succeeds, the method call will return a model class for the end point you called. This model in turn contains multiple record classes, each of which represents part of the data returned by the web service.

If the request fails, the client class throws an exception.

//Write Your Python Script

import requests

r = requests.get('https://get.geojs.io/')
ip_request = requests.get('https://get.geojs.io/v1/ip.json')
ipAdd = ip_request.json()['ip']

print(ipAdd)
url = 'https://get.geojs.io/v1/ip/geo/'+ ipAdd +'.json' 

geo_request = requests.get(url)
geo_data = geo_request.json()
print(geo_data)
print(geo_data['city'])
print(geo_data['organization_name'])

noimg Visit Link: digitalocean.com