Introduction to geolocation

One way to obtain geolocation from an ip address or domain is using a service that provides this kind of information. Among the services that provide this information, we can highlight hackertarget.com (https://hackertarget.com/geoip-ip-location-lookup/).

With hackertarget.com, we can get a geolocation from an ip address:

This service also provides a REST API for obtaining a geolocation from an ip address: https://api.hackertarget.com/geoip/?q=8.8.8.8.

Another service is api.hostip.info, which provides a query by ip address:

In the next script, we are using this service and the requests module to obtain a json response with the information for geolocation.

You can find the following code in the ip_to_geo.py file:

import requests

class IPtoGeo(object):

def __init__(self, ip_address):

# Initialize objects to store
self.latitude = ''
self.longitude = ''
self.country = ''
self.city = ''
self.ip_address = ip_address
self._get_location()

def _get_location(self):
json_request = requests.get('http://api.hostip.info/get_json.php ip=%s&position=true' % self.ip_address).json()

self.country = json_request['country_name']
self.country_code = json_request['country_code']
self.city = json_request['city']
self.latitude = json_request['lat']
self.longitude = json_request['lng']

if __name__ == '__main__':
ip1 = IPtoGeo('8.8.8.8')
print(ip1.__dict__)

This is the output of the previous script:

{'latitude': '37.402', 'longitude': '-122.078', 'country': 'UNITED STATES', 'city': 'Mountain View, CA', 'ip_address': '8.8.8.8', 'country_code': 'US'}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.60.158