appliedsec / pygeoip

DEPRECATED: Pure Python API for Maxmind's binary GeoIP databases
https://github.com/appliedsec/pygeoip
GNU Lesser General Public License v3.0
481 stars 111 forks source link

Add GeoIP.last_netmask property #47

Closed palant closed 11 years ago

palant commented 11 years ago

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.

tiwilliam commented 11 years ago

Thanks for the idea. I've implemented it as a function to mock the Maxmind Python and C API. Would be nice to add GeoIP.range_by_ip() now when we have GeoIP.last_netmask().