3

I was able to get geolite2 working on python2.7 - but i needed 3.4. I found the instructions for 2.7 on this link: http://pythonhosted.org/python-geoip/. Code fragments are also provided.

pip install python-geoip
pip install python-geoip-geolite2
>>> from geoip import geolite2
>>> match = geolite2.lookup('17.0.0.1')
>>> match is not None
True

So I naturally changed all the pip to pip3 and installed on a fresh VM. There is no error on the code but it does not looup and return values from their db.

pip3 install python-geoip
pip3 install python-geoip-geolite2

In the 2.7 VM, when I used 3.4, I pointed the geoip lib at /usr/local/lib/python2.7/dist-packages - no luck either. It however works on 2.7 on the same VM.

What should I need to do to make it to work on 3.4?

user5331677
  • 409
  • 3
  • 6
  • 12

4 Answers4

8

python-geoip does not support Python 3 and has not been updated in two years. Although there is a pull request to add Python 3 support, I would not expect it to be merged and released any time soon. I would recommend using the official MaxMind geoip2 package instead.

Greg Oschwald
  • 1,716
  • 2
  • 11
  • 14
7

Install:

apt install python3-pip
pip3 install maxminddb 
pip3 install maxminddb-geolite2

Example use:

#!/usr/bin/python3
# coding=utf-8

from geolite2 import geolite2

reader = geolite2.reader()

# google's ip
match = reader.get('172.217.16.163')
if match:
  # print(match)
  if 'country' in match:
    print(match['country']['iso_code'])
  else:
    print(match['continent']['code'])
else:
  print('') 
Community
  • 1
  • 1
Chris42
  • 119
  • 1
  • 3
3

python-geoip with python 3 support (install with pip or pip3):

pip3 install python-geoip-python3

output:

Collecting python-geoip-python3
  Downloading python_geoip_python3-1.3-py2.py3-none-any.whl (7.4 kB)
Installing collected packages: python-geoip-python3
Successfully installed python-geoip-python3-1.3

for geolite2:

pip3 install python-geoip-geolite2

output:

Successfully built python-geoip-geolite2
Installing collected packages: python-geoip-geolite2
Successfully installed python-geoip-geolite2-2015.303

example with test_geoip.py:

#!/usr/bin/env python3

import socket
from geoip 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))

match = geolite2.lookup(ip_address)

if match is not None:
    print('Country: ',match.country)
    print('Continent: ',match.continent)
    print('Time zone: ', match.timezone)
    print('Location: ', match.location)

run:

python3 test_geoip.py --hostname=amazon.co.uk

output:

IP address: 178.236.7.220
Country:  IE
Continent:  EU
Time zone:  Europe/Dublin
Location:  (53.3478, -6.2597)

run again:

python3 test_geoip.py --hostname=amazon.co.uk

output:

IP address: 54.239.34.171
Country:  US
Continent:  NA
Time zone:  America/Los_Angeles
Location:  (47.6103, -122.3341)
Milovan Tomašević
  • 6,823
  • 1
  • 50
  • 42
-1

To use the maxmind db:

Install:

maxminddb 
maxminddb-geolite

In your code:

from geolite2 import geolite2

match = geolite2.reader()
geoip = match.get('xxx.xxx.xxx.xxx')
Das_Geek
  • 2,775
  • 7
  • 20
  • 26
M Newton
  • 29
  • 6
  • to me it seems the code provided mixes something up as the code-sections `geolite2` import does not seem to belong to the package `maxminddb`? `maxminddb-geolite` was not installable for me though `pipenv` which i suspect might have to do with maxmind changing their licensing policy and no longer offering the geolite-db to unregistered users. note that there is also a pypi-package `geolite2` which does a similar thing, is also provided by maxmind and has `geolite2` defined in the packages top_level.txt. – antiplex Mar 03 '20 at 13:17