This is a native GeoIP.dat reader for Python. It allows the use of GeoIP without an external service, or Datastore, from Google App Engine.
Cold start takes about 5ms to open the database. Warm lookups are so fast they can't reliably be measured (an urlfetch will cost ~100 ms, a single Datastore get ~20ms).
Note that the returned CIDR prefixes aren't reliable. Suggest not using them for abuse prevention!
The database is cached in memory. This adds 1.1mb to your interpreter size!
ExampleEnsure GeoIP.dat (download) and pygeoip.py (download) are in your AppEngine project directory.
from google.appengine.ext import webapp
# Load the database once and store it globally in interpreter memory.
GEOIP = pygeoip.Database('GeoIP.dat')
info = GEOIP.lookup(self.request.remote_addr)
if not info.country:
msg = 'Could not locate country for ' + self.request.remote_addr
msg = 'Country for %s is %s' % (self.request.remote_addr, info.country)
Copyright © 2013 Black Duck Software, Inc. and its contributors, Some Rights Reserved. Unless otherwise marked, this work is licensed under a Creative Commons Attribution 3.0 Unported License . Ohloh ® and the Ohloh logo are trademarks of Black Duck Software, Inc. in the United States and/or other jurisdictions. All other trademarks are the property of their respective holders.