Skip to content

IP - geoiplookup.py

note, it may be slow, better use the bash version

input file (ips.txt)

111.222.333.444
111.222.333.444
111.222.333.444
111.222.333.444

run

./geoiplookup.py ips.txt > ip_country.txt

ipcheck.py script

#!/usr/bin/env python3
# https://github.com/christophetd/geolocate-ips

import sys

import requests

if len(sys.argv) < 2:
    sys.stderr.write('Usage: python geolocate.py ip_file\n')
    sys.stderr.write('ip_file should be a file containing one IP per line\n')
    exit(1)


def read_ip_list():
    ip_file = sys.argv[1]

    try:
        with open(ip_file) as file:
            return file.read().splitlines()
    except IOError:
        sys.stderr.write('Unable to open IP file {}\n'.format(ip_file))
        exit(1)


def get_ip_country(ip):
    response = requests.get('http://ip2c.org/{}'.format(ip))
    error_codes = ['WRONG INPUT', 'UNKNOWN']  # http://about.ip2c.org/#outputs

    country_name = response.text.split(';')[3]

    if response.status_code != 200 or country_name in error_codes:
        sys.stderr.write('Could not retrieve any geolocation info for IP {} - skipping.\n'.format(ip))
        return False

    if country_name == 'Reserved':
        sys.stderr.write('Could not retrieve any geolocation for IP {} - it looks like a private address\n'.format(ip))
        return False

    country_code = response.text.split(';')[1]

    return {
        'country_name': country_name,
        'country_code': country_code
    }


ip_list = read_ip_list()
for ip in ip_list:
    result = get_ip_country(ip)
    if result:
        print("{},{},{}".format(ip, result['country_code'], result['country_name']))

output file (ip_country.txt)

111.222.333.444,CN,China
111.222.333.444,MX,Mexico
111.222.333.444,BR,Brazil
111.222.333.444,ML,Mali