Giter VIP home page Giter VIP logo

pygeoip's Introduction

No Maintenance Intended

DEPRECATED

This project is no longer supported. Please consider using Maxmind's GeoIP2 Python API.

Pure Python GeoIP API

This library is based on Maxmind's GeoIP C API.

Tested with Python version 2.6, 2.7, 3.2, 3.3 and 3.4.

Build Status Coverage Status Downloads

Installation

You can easily install pygeoip from PyPi.

pip install pygeoip

Issues and Contribution

Bug reports are done by creating an issue on Github. If you want to contribute you can always create a pull request for discussion and code submission.

Documentation

For more information, check out the documentation over at Read the Docs.

pygeoip's People

Contributors

chrish42 avatar daybarr avatar ioc32 avatar iorlas avatar ironholds avatar it-appliedsec avatar jennifere avatar kzyapkov avatar likema avatar ludovic-gasc avatar msherry avatar oschwald avatar ralphbean avatar stevetu avatar techee avatar tiwilliam avatar wmalinowski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pygeoip's Issues

pygeoip._get_record returns None when country is not found, shoul return empty dict.

Functions that call _get_record expect a dict as return value, if None is returned an exception is raised.
The fix is just replacing "return None" by "return {}" in _init._get_record:321.

Stack trace of the error:

File "/pygeoip/init.py", line 430, in country_code_by_addr
return self.region_by_addr(addr)['country_code']

File "/pygeoip/init.py", line 588, in region_by_addr
return self._get_region(ipnum)

File "/pygeoip/init.py", line 296, in _get_region
country_code = rec['country_code'] if 'country_code' in rec else ''

TypeError: argument of type 'NoneType' is not iterable

ValueError: seek out of range

As can be seen below, using the GeoIP2-Country database on PyPy throws an error when looking up an IP.

>>>> import pygeoip
>>>> reader = pygeoip.GeoIP('GeoIP2-Country.mmdb')
>>>> reader.country_code_by_addr('95.97.5.194')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pepijn/code/pyfnlogstat/env/site-packages/pygeoip/__init__.py", line 434, in country_code_by_addr
    country_id = self.id_by_addr(addr)
  File "/home/pepijn/code/pyfnlogstat/env/site-packages/pygeoip/__init__.py", line 418, in id_by_addr
    return self._seek_country(ipnum) - const.COUNTRY_BEGIN
  File "/home/pepijn/code/pyfnlogstat/env/site-packages/pygeoip/__init__.py", line 213, in _seek_country
    self._fp.seek(startIndex, os.SEEK_SET)
ValueError: seek out of range

Instantiating GeoIp second time takes forever

Hi,

I am not sure it is a right behavior, but when I instantiate GeoIP second time(first time incorrect file location) it take forever.

Here is the traceback-

>>> from pygeoip import GeoIP
>>> geoip = GeoIP('/usr/share/GeoIP/GeoIP.dat')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.linux-x86_64/egg/pygeoip/__init__.py", line 77, in __call__

  File "build/bdist.linux-x86_64/egg/pygeoip/__init__.py", line 121, in __init__

  File "/home/prakash/.virtualenvs/nereid-crm/lib/python2.7/codecs.py", line 881, in open
    file = __builtin__.open(filename, mode, buffering)
IOError: [Errno 2] No such file or directory: '/usr/share/GeoIP/GeoIP.dat'
>>> geoip = GeoIP('/usr/local/Cellar/geoip/1.4.8/share/GeoIP/GeoIP.dat)

It hangs after this,

Error installing on Python3.3 + FreeBSD

$ /usr/local/lib# easy_install-3.3 pygeoip
Searching for pygeoip
Reading http://pypi.python.org/simple/pygeoip/
Reading http://code.google.com/p/pygeoip/
Reading https://github.com/appliedsec/pygeoip
Best match: pygeoip 0.2.6
Downloading http://pypi.python.org/packages/source/p/pygeoip/pygeoip-0.2.6.zip#md5=b3ac1bfcd535782bc59af78e722cf5c1
Processing pygeoip-0.2.6.zip
Writing /tmp/easy_install-fch_ox/pygeoip-0.2.6/setup.cfg
Running pygeoip-0.2.6/setup.py -q bdist_egg --dist-dir /tmp/easy_install-fch_ox/pygeoip-0.2.6/egg-dist-tmp-1vpec1
Traceback (most recent call last):
  File "/usr/local/bin/easy_install-3.3", line 9, in <module>
    load_entry_point('distribute==0.6.35', 'console_scripts', 'easy_install-3.3')()
  File "/usr/local/lib/python3.3/site-packages/distribute-0.6.35-py3.3.egg/setuptools/command/easy_install.py", line 1937, in main
    with_ei_usage(lambda:
  File "/usr/local/lib/python3.3/site-packages/distribute-0.6.35-py3.3.egg/setuptools/command/easy_install.py", line 1918, in with_ei_usage
    return f()
  File "/usr/local/lib/python3.3/site-packages/distribute-0.6.35-py3.3.egg/setuptools/command/easy_install.py", line 1941, in <lambda>
    distclass=DistributionWithoutHelpCommands, **kw
  File "/usr/local/lib/python3.3/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/local/lib/python3.3/distutils/dist.py", line 917, in run_commands
    self.run_command(cmd)
  File "/usr/local/lib/python3.3/distutils/dist.py", line 936, in run_command
    cmd_obj.run()
  File "/usr/local/lib/python3.3/site-packages/distribute-0.6.35-py3.3.egg/setuptools/command/easy_install.py", line 358, in run
    self.easy_install(spec, not self.no_deps)
  File "/usr/local/lib/python3.3/site-packages/distribute-0.6.35-py3.3.egg/setuptools/command/easy_install.py", line 598, in easy_install
    return self.install_item(spec, dist.location, tmpdir, deps)
  File "/usr/local/lib/python3.3/site-packages/distribute-0.6.35-py3.3.egg/setuptools/command/easy_install.py", line 628, in install_item
    dists = self.install_eggs(spec, download, tmpdir)
  File "/usr/local/lib/python3.3/site-packages/distribute-0.6.35-py3.3.egg/setuptools/command/easy_install.py", line 823, in install_eggs
    return self.build_and_install(setup_script, setup_base)
  File "/usr/local/lib/python3.3/site-packages/distribute-0.6.35-py3.3.egg/setuptools/command/easy_install.py", line 1103, in build_and_install
    self.run_setup(setup_script, setup_base, args)
  File "/usr/local/lib/python3.3/site-packages/distribute-0.6.35-py3.3.egg/setuptools/command/easy_install.py", line 1089, in run_setup
    run_setup(setup_script, args)
  File "/usr/local/lib/python3.3/site-packages/distribute-0.6.35-py3.3.egg/setuptools/sandbox.py", line 34, in run_setup
    lambda: exec(compile(open(
  File "/usr/local/lib/python3.3/site-packages/distribute-0.6.35-py3.3.egg/setuptools/sandbox.py", line 82, in run
    return func()
  File "/usr/local/lib/python3.3/site-packages/distribute-0.6.35-py3.3.egg/setuptools/sandbox.py", line 35, in <lambda>
    "setup.py"
  File "/usr/local/lib/python3.3/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1098: ordinal not in range(128)

Correctly using pygeoip inside a Django app

Hi, I want to use pygeoip library in one of my Django projects so as to know visitor's city and then offering him dynamic webpage based on the city. So these are my queries -

  1. What should be the database format - binary or csv? - http://dev.maxmind.com/geoip/legacy/geolite/
  2. I am using pygeoip in one of the views inside a Django app. What's the best place to keep the database inside the Django project? Thanks.

I cant load MindMax database

I downloaded and tried with both GeoLite2 CSV and MMDB database file but nothing worked.
The error is:

Traceback (most recent call last):
  File "test.py", line 71, in <module>
    benchmark_pygeoip()
  File "test.py", line 61, in benchmark_pygeoip
    g.country_name_by_addr(ip)
  File "/Library/Python/2.7/site-packages/pygeoip/__init__.py", line 488, in country_name_by_addr
    country_id = self.id_by_addr(addr)
  File "/Library/Python/2.7/site-packages/pygeoip/__init__.py", line 422, in id_by_addr
    return self._seek_country(ipnum) - const.COUNTRY_BEGIN
  File "/Library/Python/2.7/site-packages/pygeoip/__init__.py", line 241, in _seek_country
    raise GeoIPError('Corrupt database')
pygeoip.GeoIPError: Corrupt database

IPv6 support

Add IPv6 support: country_code_by_addr_v6, etc, etc.

Would also be nice to have IPv6 to IPv4 mapping for 6to4 (2002::/16) and Teredo (2001::/32) - note that for Teredo you should probably use the client address instead of the server address.

Cache db file in a in-memory Key-value store?

Hi,

I wonder if it's possible to cache the binary Maxmind db file in a key-value store like Redis/Memcached?

Open the binary file in each connection seems not good enough for me. I hope the GeoIP(GeoIPBase) class init could accept any file-like object instead of file path or flag.

error: illegal IP address string passed to inet_pton

error: illegal IP address string passed to inet_pton
  File "django/core/handlers/base.py", line 92, in get_response
    response = middleware_method(request)
  File "newrelic/hooks/framework_django.py", line 223, in wrapper
    return wrapped(*args, **kwargs)
  File "easy_timezones/middleware.py", line 42, in process_request
    tz = db.time_zone_by_addr(ip)
  File "pygeoip/__init__.py", line 583, in time_zone_by_addr
    ipnum = util.ip2long(addr)
  File "pygeoip/util.py", line 39, in ip2long
    return int(binascii.hexlify(socket.inet_pton(socket.AF_INET6, ip
 )), 16)

I suppose it is a bug in pygeoip. We have been getting this crash for sometime in our server. Can you please suggest a workaround or a publish a fix.

GeoIPError at / Invalid database type; record_* methods expect City database

have done nothing but upgraded from 0.2.2 to 0.2.3 and got this Django crash.

What's wrong?

Request Method: GET
Request URL: http://staging.thesolitaires.com/
Django Version: 1.3.1
Exception Type: GeoIPError
Exception Value:
Invalid database type; record_* methods expect City database
Exception Location: /home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/pygeoip-0.2.3-py2.6.egg/pygeoip/init.py in record_by_addr, line 543
Python Executable: /usr/bin/python
Python Version: 2.6.5

Python Path:
['/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/Django-1.3.1-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/gameportal-0.9.1.1_806_g1842eac-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/wsclient-0.9.1.1_806_g1842eac-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/kgputils-0.9.1.1_806_g1842eac-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/django_profiles-0.2-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/django_simple_captcha-0.3.1-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/django_cms-2.1.2-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/djangorestframework-0.3.3-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/South-0.7.4-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/oauth2-1.5.211-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/python_openid-2.2.5-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/pycrypto-2.3-py2.6-linux-i686.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/zope.interface-3.8.0-py2.6-linux-i686.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/pygeoip-0.2.3-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/psycopg2-2.4.5-py2.6-linux-i686.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/iso8601-0.1.4-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/lxml-2.3.4-py2.6-linux-i686.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/ConcurrentLogHandler-0.8.4-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/httplib2-0.7.4-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/Twisted-12.0.0-py2.6-linux-i686.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/pyOpenSSL-0.12-py2.6-linux-i686.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/pyserial-2.6-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/thrift-0.8.0-py2.6-linux-i686.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/IPy-0.75-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/stomper-0.2.4-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/simplejson-2.5.0-py2.6-linux-i686.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/telephus-0.7.1-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/django_registration-0.8_alpha_1-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/django_socialregistration-0.4.5-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/facebook_python_sdk-0.1.1-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/python_daemon-1.6-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/python_memcached-1.48-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/gunicorn-0.14.2-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/Pillow-1.7.7-py2.6-linux-i686.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/recipes-0.9.1.1_806_g1842eac-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/zc.recipe.egg-1.3.2-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/zc.buildout-1.5.2-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/lockfile-0.9.1-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/setuptools-0.6c11-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/six-1.1.0-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/URLObject-2.0.1-py2.6.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/PIL-1.1.7-py2.6-linux-i686.egg',
'/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/django_classy_tags-0.3.4.1-py2.6.egg',
'/usr/lib/python26.zip',
'/usr/lib/python2.6',
'/usr/lib/python2.6/plat-linux2',
'/usr/lib/python2.6/lib-tk',
'/usr/lib/python2.6/lib-old',
'/usr/lib/python2.6/lib-dynload',
'/usr/lib/python2.6/site-packages']
Server time: Thu, 12 Apr 2012 10:57:34 +0000

all trace below:

Environment:

Request Method: GET
Request URL: http://staging.thesolitaires.com/

Django Version: 1.3.1
Python Version: 2.6.5
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admindocs',
'django.contrib.admin',
'django.contrib.humanize',
'django.contrib.sitemaps',
'cms',
'cms.plugins.text',
'cms.plugins.picture',
'cms.plugins.link',
'cms.plugins.file',
'cms.plugins.snippet',
'mptt',
'publisher',
'menus',
'registration',
'profiles',
'captcha',
'socialregistration',
'south',
'djangorestframework',
'gunicorn',
'gameportal.portal',
'gameportal.players',
'gameportal.fbapp',
'gameportal.chat',
'gameportal.money',
'gameportal.eyecon',
'gameportal.aquapay',
'gameportal.sessiontracking',
'gameportal.geoip',
'gameportal.solver',
'gameportal.cron',
'gameportal.maintenance',
'gameportal.articles',
'gameportal.userprofile',
'gameportal.cashier',
'gameportal.blacklist',
'gameportal.default_avatars',
'gameportal.contactus',
'gameportal.g2s',
'gameportal.notifications']
Installed Middleware:
('gameportal.geoip.IPCheckMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'djangoext.log.LoggingMiddleware',
'socialregistration.middleware.FacebookMiddleware',
'django.middleware.doc.XViewMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.transaction.TransactionMiddleware',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.media.PlaceholderMediaMiddleware',
'gameportal.maintenance.middleware.MaintenanceModeMiddleware',
'gameportal.players.middleware.PlayerMiddleware',
'gameportal.sessiontracking.middleware.SessionTrackingMiddleware')

Traceback:
File "/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/Django-1.3.1-py2.6.egg/django/core/handlers/base.py" in get_response

  1.                 response = middleware_method(request)
    
    File "/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/gameportal-0.9.1.1_806_g1842eac-py2.6.egg/gameportal/geoip/middleware.py" in process_request
  2.     if not is_allowed(request.META['REMOTE_ADDR']):
    
    File "/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/kgputils-0.9.1.1_806_g1842eac-py2.6.egg/memoize.py" in wrap
  3.         _cache[args] = ret = fn(*args)
    
    File "/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/gameportal-0.9.1.1_806_g1842eac-py2.6.egg/gameportal/geoip/models.py" in is_allowed
  4. gdata = get_geo_data(addr)
    
    File "/home/kgp/kgp-0.9.1.1-806-g1842eac/eggs/pygeoip-0.2.3-py2.6.egg/pygeoip/init.py" in record_by_addr
  5.             raise GeoIPError('Invalid database type; record_\* methods expect City database')
    

Exception Type: GeoIPError at /
Exception Value: Invalid database type; record_* methods expect City database

GeoIPRegion.dat queries don't work for outside US and Canada

What steps will reproduce the problem?

  1. Grab GeoIPRegion.dat from Maxgrind
  2. country1 = country_code_by_name('france2.fr')
  3. country2 = country_code_by_addr('212.23.176.132')

What is the expected output? What do you see instead?
country1 and country2 should both be 'FR'. It returns empty.

What version of the product are you using? On what operating system?
Using pygeoip 0.2.2, reproduced on windows and linux platforms.

Please provide any additional information below.
This is working if I'm using the official python package from maxgrind. Another IP address in India (122.174.46.61) is also never resolved. Working with GeoLiteCity.
IP from Canada or US seem to be resolved correctly.

Delete comment Comment 1 by nahor.j, Nov 4, 2011
I believe the issue is in _get_region:
if i in const.COUNTRY_CODES

I assume that this code is supposed to verify that i is inside the array boundary limits. But what it really does is test if i is a value in the array, which is always false.

And while I'm at it, there is this code in a couple of places in the same function:
(seek_region // 1000) // 26
which I think should be:
(seek_region - 1000) // 26
Delete comment

Exception handler cannot make up its mind.

Using the netspeeds database (specifically "Binary format for APIs with Cellular") we get:

test.netspeed_by_addr("2600:1006:b025:fad7:b945:d20a:9451:85d")Traceback (most recent call last):
File "", line 1, in
File "/home/ironholds/.local/lib/python2.7/site-packages/pygeoip-0.3.1-py2.7.egg/pygeoip/init.py", line 456, in netspeed_by_addr
return const.NETSPEED_NAMES[self.id_by_addr(addr)]
File "/home/ironholds/.local/lib/python2.7/site-packages/pygeoip-0.3.1-py2.7.egg/pygeoip/init.py", line 415, in id_by_addr
raise GeoIPError('Invalid database type; expected IPv4 address')
pygeoip.GeoIPError: Invalid database type; expected IPv4 address
test.netspeed_by_addr("203.0.113.30")
Traceback (most recent call last):
File "", line 1, in
File "/home/ironholds/.local/lib/python2.7/site-packages/pygeoip-0.3.1-py2.7.egg/pygeoip/init.py", line 456, in netspeed_by_addr
return const.NETSPEED_NAMES[self.id_by_addr(addr)]
File "/home/ironholds/.local/lib/python2.7/site-packages/pygeoip-0.3.1-py2.7.egg/pygeoip/init.py", line 413, in id_by_addr
raise GeoIPError('Invalid database type; expected IPv6 address')
pygeoip.GeoIPError: Invalid database type; expected IPv6 address

...so it wants IPv4 except when you provide one. Is this expected behaviour/an unsupporteddataset,or..?

0.2.7 on PyPI

Thanks to all for the hard work on this library; I was just wondering if 0.2.7 could be made available on PyPI? Is this one for you @jennifere ?

Thanks,
Steven.

Release 0.3.2

We're ready to upload https://github.com/appliedsec/pygeoip/tree/v0.3.2 to PyPi with following updates:

  • New: Support for Python 3.4
  • New: Support for NetSpeedCell databases (Thanks to Gregory Oschwald)
  • Fix: Improved exception handling (Thanks to Oliver Keyes)
  • Fix: Return country for region lookups outside US and CA

Works with Python 3.4

FYI - Your module works with Python 3.4 (at least on my system). Feel free to update the various documents and your Travis-CI tests.

region_name not available

The official Python driver offers region_name which is the full name for what you call region_code, i.e. "Californa" instead of "CA".

>>> city.record_by_addr('64.233.161.99')
>>> {'area_code': 650,
     'city': 'Mountain View',
     'country_code': 'US',
     'country_code3': 'USA',
     'country_name': 'United States',
     'dma_code': 807,
     'latitude': 37.419200897216797,
     'longitude': -122.05740356445312,
     'metro_code': 807,
     'postal_code': '94043',
     'region': 'CA',
     'region_name': 'California',
     'time_zone': 'America/Los_Angeles'}

Is there any way to get this with pygeoip as well? Couldn't find anything except from the 0.3.0 changelog which hinted that you renamed region_name to region_code. But that's not the same.

Invalid region name

I didn't actually try but this code doesn't look right:
region = ''.join([chr((seek_region // 1000) // 26 + 65), chr((seek_region // 1000) % 26 + 65)])

I believe it should be:
region = ''.join([chr((seek_region - 1000) // 26 + 65), chr((seek_region - 1000) % 26 + 65)])

which makes more sense and is what the C code does:
region->region[0] = (char) ((seek_region - 1000)/26 + 65);
region->region[1] = (char) ((seek_region - 1000)%26 + 65);

Mo netspeeds, mo problems

The netspeeds databases /should/ now be integrated (thanks for that!) but are bugging out on IPs:

ip4_geo = pygeoip.GeoIP(filename = "/usr/share/GeoIP/GeoIPNetSpeed.dat", flags = 1)
ip6_geo = pygeoip.GeoIP(filename = "/usr/share/GeoIP/GeoIPNetSpeedCell.dat", flags = 1)

ip6_geo.netspeed_by_addr("‎42.201.247.243")
Traceback (most recent call last):
File "", line 1, in
File "build/bdist.linux-x86_64/egg/pygeoip/init.py", line 464, in netspeed_by_addr
File "build/bdist.linux-x86_64/egg/pygeoip/util.py", line 39, in ip2long
socket.error: illegal IP address string passed to inet_pton
ip4_geo.netspeed_by_addr("‎42.201.247.243")
Traceback (most recent call last):
File "", line 1, in
File "build/bdist.linux-x86_64/egg/pygeoip/init.py", line 461, in netspeed_by_addr
File "build/bdist.linux-x86_64/egg/pygeoip/init.py", line 421, in id_by_addr
File "build/bdist.linux-x86_64/egg/pygeoip/util.py", line 39, in ip2long
socket.error: illegal IP address string passed to inet_pton

Am I missing something crucial, or...?

Release v0.3.1

We are now using readthedocs.org, to align the code with the documentation we need to update PyPi.

Ping @jennifere 😄

Unicode encoded data returned as ascii encoded string.

Reproduction Steps:

  1. Run some code which fetches a GeoIP entry with unicode characters:

gi = pygeoip.GeoIP('MyPathToGeoIPOrganizationFile')
org = gi.org_by_addr('67.215.9.186')
sorg = u'org = %s' % org

Expected output:

org = Tirabout François

Actual Output:

<type 'exceptions.UnicodeDecodeError'> 'ascii' codec can't decode byte 0xc3 in position 13: ordinal not in range(128)

Versions:

OS: Windows Vista
Python: 2.7.2
pygeoip: 2.2

Workaround:

Force pygeoip to use unicode encoded strings as follows:

  1. Create file in [PYTHON]\Lib\site-packages\sitecustomize.py with the following contents:

import sys
sys.setdefaultencoding('utf8')

Inconsistent / missing results from Region database

This is using latest db dump from maxmind: GeoIPRegion-115_20141021

It generally works as seen here:

Ruby:

pry(main)> GEOIP_DB.region('96.45.202.230')
=> #<struct GeoIP::Region
 request="96.45.202.230",
 ip="96.45.202.230",
 country_code2="CA",
 country_code3="CAN",
 country_name="Canada",
 continent_code="NA",
 region_code="ON",
 region_name="Ontario",
 timezone="America/Rainy_River">

Python:

>>> print gi.region_by_addr('96.45.202.230')
{'region_code': 'ON', 'country_code': 'CA'}

But then I started to notice a lot of IPs that are not matched to anything in pygeoip. For example:

Ruby:

pry(main)> GEOIP_DB.region('46.22.192.1')
=> #<struct GeoIP::Region
 request="46.22.192.1",
 ip="46.22.192.1",
 country_code2="FR",
 country_code3="FRA",
 country_name="France",
 continent_code="EU",
 region_code="",
 region_name=nil,
 timezone="Europe/Paris">

Python:

print gi.region_by_addr('46.22.192.1')
{'region_code': None, 'country_code': None}

Any idea what migth be the problem?

MEMORY_CACHE requires db to be gzipped

pygeoip 0.2.3 when using pygeoip.MEMORY_CACHE requires the database to be gzipped now, and if it's not throws an IOError: Not a gzipped file

Worked fine in 0.2.2

"pygeoip __init__.py _gethostbyname" Error.

Hello,

I hope you guys know there is a python IRC bot Willie, To make his .ip command work we need pygeoip, I've installed it and its working but when I type.

@Bhai .ip Bhai
gaierror: [Errno -2] Name or service not known (file "/usr/local/lib/python2.7/dist-packages/pygeoip/init.py", line 394, in _gethostbyname)

This Error, Any way to fix it?

GeoIPError: Invalid database type; record_* methods expect City database

I have following warning when I try to use latest city database:

env/src/pygeoip/pygeoip/__init__.py:128: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
if delim == six.u(chr(255) * 3):

and when I try to execute:

record = geo_ip.record_by_addr('24.24.24.24')

I get following exception:

GeoIPError: Invalid database type; record_* methods expect City database

But with following code:

if delim == chr(255) * 3:

record_by_addr() works as expected.

Add GeoIP.last_netmask property

MaxMind's Python API has a GeoIP.last_netmask property starting with version 1.2.4: https://github.com/maxmind/geoip-api-python/blob/master/ChangeLog. This allows determining which IP range a particular IP address belongs to and for example iterating over the entire IP space (start with 1.0.0.0, look up, increase IP address according to last_netmask, repeat). This attribute can be set in the _seek_country() method with a single additional line:

             for depth in range(seek_depth, -1, -1):
+                self.last_netmask = seek_depth - depth + 1
                 if self._flags & const.MEMORY_CACHE:

That's a hack of course, you would normally only set that attribute when returning and also declare that property somewhere - but that hack already works.

timezones are not IANA standard

I found that I needed to create a mapping from the timezones returned by pygeoip to the proper ones in pytz so I thought I'd share it here. The keys are what are returned by pygeoip and the values are the proper IANA timezones for each.

{
    'Africa/Asmera': 'Africa/Nairobi',
    'America/Indianapolis': 'America/Indiana/Indianapolis',
    'America/Montreal': 'America/Toronto',
    'Asia/Calcutta': 'Asia/Kolkata',
    'Asia/Chongqing': 'Asia/Shanghai',
    'Asia/Harbin': 'Asia/Shanghai',
    'Asia/Istanbul': 'Europe/Istanbul',
    'Asia/Katmandu': 'Asia/Kathmandu',
    'Asia/Macao': 'Asia/Macau',
    'Atlantic/Faeroe': 'Atlantic/Faroe',
    'Australia/Canberra': 'Australia/Sydney',
    'Australia/NSW': 'Australia/Sydney',
    'Australia/North': 'Australia/Darwin',
    'Australia/Queensland': 'Australia/Brisbane',
    'Australia/South': 'Australia/Adelaide',
    'Australia/Tasmania': 'Australia/Hobart',
    'Australia/Victoria': 'Australia/Melbourne',
    'Australia/West': 'Australia/Perth',
    'Chile/Continental': 'America/Santiago',
    'Pacific/Samoa': 'Pacific/Pago_Pago',
    'US/Samoa': 'Pacific/Pago_Pago',
}

UnicodeWarning in pygeoip/__init__.py:128

Getting a UnicodeWarning in pygeoip/init.py:128 when using MEMORY_CACHE and city database. Can't retrieve any city/region data as a result of this.

It works fine for STANDARD, and also worked fine in pygeoip 0.2.2 with MEMORY_CACHE

here's the error:
/Library/Python/2.7/site-packages/pygeoip/init.py:128: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal

Error under ipython console

In [158]: gi.country_name_by_addr('64.233.161.99')

/usr/local/lib/python2.7/dist-packages/pygeoip/init.pyc in country_name_by_addr(self, addr)
488 VALID_EDITIONS = (const.COUNTRY_EDITION, const.COUNTRY_EDITION_V6)
489 if self._databaseType in VALID_EDITIONS:
--> 490 return const.COUNTRY_NAMES[self.id_by_addr(addr)]
491 elif self._databaseType in const.CITY_EDITIONS:
492 return self.record_by_addr(addr)['country_name']

/usr/local/lib/python2.7/dist-packages/pygeoip/init.pyc in id_by_addr(self, addr)
427 raise GeoIPError(message)
428
--> 429 return self._seek_country(ipnum) - const.COUNTRY_BEGIN
430
431 def country_code_by_addr(self, addr):

/usr/local/lib/python2.7/dist-packages/pygeoip/init.pyc in _seek_country(self, ipnum)
223 for i in range(2):
224 for j in range(self._recordLength):
--> 225 byte = buf[self._recordLength * i + j]
226 x[i] += ord(byte) << (j * 8)
227 if ipnum & (1 << depth):

IndexError: string index out of range

~ % python --version
Python 2.7.3

~ % gcc --version
gcc (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2

~ % cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.10
DISTRIB_CODENAME=quantal
DISTRIB_DESCRIPTION="Ubuntu 12.10"

pygeoip.GeoIPError: Invalid database type, expected Org, ISP or ASNum

I'm getting an error when trying to use pygeoip's org_by_name(self, hostname), but other functions work fine (as far as I've tested).

Tried on both Ubuntu 12.10 and Debian 6 Squeeze, with both geoip-database from their respective repositories and manual download from GeoIP Country Database Installation Instructions.

Python version is 2.7.3 in both cases
pygeoip is newest from PyPi, 0.2.6

>>> import pygeoip
>>> gi = pygeoip.GeoIP("/usr/share/GeoIP/GeoIP.dat", pygeoip.MEMORY_CACHE)
>>> gi.country_code_by_name("thevoid.no")
'DE'
>>> gi.org_by_name("thevoid.no")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pygeoip/__init__.py", line 548, in org_by_name
    return self.org_by_addr(addr)
  File "/usr/local/lib/python2.7/dist-packages/pygeoip/__init__.py", line 531, in org_by_addr
    raise GeoIPError(message)
GeoIPError: Invalid database type, expected Org, ISP or ASNum

I don't know if there's a problem with pygeoip or the database.

IPV6 results returned on database not supporting IPV6

When running query for IPV6 against GEOIPCITY maxmind db I am getting results that are from the wrong location, querying maxmind support they state that this database doesnt actually support IPV6.

the database is GeoIP-133_20150317.tar.gz

I am using the current pygeoip installed via pip running on ubuntu

here is a sample output for the code we are using

d = geo_test.get(data removed*)

d

['Hong Kong', 'na', '22.2833', '114.15', '0']

the function actually being used is pretty simple

def get_locale_data(addr):
        locale = locale.record_by_addr(addr)
        if locale:
               _data = [
                        str(locale['country_name']),
                        str("na"),
                        str(locale['latitude']),
                        str(locale['longitude']),

                        ]

                return _data
        else:
                return False

Empty dictionary from one specific ip address

Hi William,

Thanks for your module, it work great except with one specific ip address. Using this ip address I'm constantly getting an empty dictionary. Any clue on this?

Best,
Phillip

import pygeoip
gic = pygeoip.GeoIP('GeoLiteCity.dat')
test = gic.record_by_addr('91.201.64.20')
print test

{}

ImportError: No module named 'pygeoip'

Hi,

I am using python 3.4.1 and I am trying to load this module in order to use it with pandas. I am receiving the follwoing error:

ImportError Traceback (most recent call last)
in ()

----> 1 import pygeoip
2 gi = pygeoip.GeoIP('./GeoLiteCity.dat', pygeoip.MEMORY_CACHE)

ImportError: No module named 'pygeoip'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.