The MaxMind database in Python

There are other Python modules that use the MaxMind database:

In the following script, we can see an example of how to use the maxminddb-geolite2 package.

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

#!/usr/bin/env python3

import socket
from geolite2 import geolite2
import argparse
import json

# Setup commandline arguments
parser = argparse.ArgumentParser(description='Get IP Geolocation info')
parser.add_argument('--hostname', action="store", dest="hostname", required=True)

# Parse arguments
given_args = parser.parse_args()
hostname = given_args.hostname
ip_address = socket.gethostbyname(hostname)

print("IP address: {0}".format(ip_address))

# Call geolite2
reader = geolite2.reader()
response = reader.get(ip_address)
print (json.dumps(response,indent=4))
print (json.dumps(response['continent']['names']['en'],indent=4))
print (json.dumps(response['country']['names']['en'],indent=4))
print (json.dumps(response['location']['latitude'],indent=4))
print (json.dumps(response['location']['longitude'],indent=4))
print (json.dumps(response['location']['time_zone'],indent=4))

In the following screenshot, we can see the output of the previous script in JSON format, along with the amazon.com domain:

..................Content has been hidden....................

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