Skip to content

Commit a916e73

Browse files
committed
Add support for GeoLite2 ASN database
1 parent 1d29ce6 commit a916e73

File tree

6 files changed

+89
-7
lines changed

6 files changed

+89
-7
lines changed

HISTORY.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
History
44
-------
55

6+
2.5.0
7+
++++++++++++++++++
8+
9+
* Added support for GeoLite2 ASN database.
10+
611
2.4.2 (2016-12-08)
712
++++++++++++++++++
813

README.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,22 @@ Anonymous IP Database
185185
'128.101.101.101'
186186
>>> reader.close()
187187
188+
ASN Database
189+
^^^^^^^^^^^^
190+
191+
.. code-block:: pycon
192+
193+
>>> import geoip2.database
194+
>>>
195+
>>> # This creates a Reader object. You should use the same object
196+
>>> # across multiple requests as creation of it is expensive.
197+
>>> with geoip2.database.Reader('/path/to/GeoLite2-ASN.mmdb') as reader:
198+
>>> response = reader.asn('1.128.0.0')
199+
>>> response.autonomous_system_number
200+
1221
201+
>>> response.autonomous_system_organization
202+
'Telstra Pty Ltd'
203+
188204
Connection-Type Database
189205
^^^^^^^^^^^^^^^^^^^^^^^^
190206

geoip2/database.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ def anonymous_ip(self, ip_address):
120120
return self._flat_model_for(geoip2.models.AnonymousIP,
121121
'GeoIP2-Anonymous-IP', ip_address)
122122

123+
def asn(self, ip_address):
124+
"""Get the ASN object for the IP address.
125+
126+
:param ip_address: IPv4 or IPv6 address as a string.
127+
128+
:returns: :py:class:`geoip2.models.ASN` object
129+
130+
"""
131+
return self._flat_model_for(geoip2.models.ASN, 'GeoLite2-ASN',
132+
ip_address)
133+
123134
def connection_type(self, ip_address):
124135
"""Get the ConnectionType object for the IP address.
125136

geoip2/models.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,40 @@ def __init__(self, raw):
368368
self.raw = raw
369369

370370

371+
class ASN(SimpleModel):
372+
"""Model class for the GeoLite2 ASN.
373+
374+
This class provides the following attribute:
375+
376+
.. attribute:: autonomous_system_number
377+
378+
The autonomous system number associated with the IP address.
379+
380+
:type: int
381+
382+
.. attribute:: autonomous_system_organization
383+
384+
The organization associated with the registered autonomous system number
385+
for the IP address.
386+
387+
:type: unicode
388+
389+
.. attribute:: ip_address
390+
391+
The IP address used in the lookup.
392+
393+
:type: unicode
394+
"""
395+
396+
# pylint:disable=too-many-arguments
397+
def __init__(self, raw):
398+
self.autonomous_system_number = raw.get('autonomous_system_number')
399+
self.autonomous_system_organization = raw.get(
400+
'autonomous_system_organization')
401+
self.ip_address = raw.get('ip_address')
402+
self.raw = raw
403+
404+
371405
class ConnectionType(SimpleModel):
372406
"""Model class for the GeoIP2 Connection-Type.
373407
@@ -424,7 +458,7 @@ def __init__(self, raw):
424458
self.raw = raw
425459

426460

427-
class ISP(SimpleModel):
461+
class ISP(ASN):
428462
"""Model class for the GeoIP2 ISP.
429463
430464
This class provides the following attribute:
@@ -463,10 +497,6 @@ class ISP(SimpleModel):
463497

464498
# pylint:disable=too-many-arguments
465499
def __init__(self, raw):
466-
self.autonomous_system_number = raw.get('autonomous_system_number')
467-
self.autonomous_system_organization = raw.get(
468-
'autonomous_system_organization')
500+
super(ISP, self).__init__(raw)
469501
self.isp = raw.get('isp')
470502
self.organization = raw.get('organization')
471-
self.ip_address = raw.get('ip_address')
472-
self.raw = raw

tests/data

Submodule data updated 39 files

tests/database_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ def test_anonymous_ip(self):
7676
self.assertEqual(record.ip_address, ip_address)
7777
reader.close()
7878

79+
def test_asn(self):
80+
reader = geoip2.database.Reader(
81+
'tests/data/test-data/GeoLite2-ASN-Test.mmdb')
82+
83+
ip_address = '1.128.0.0'
84+
record = reader.asn(ip_address)
85+
self.assertEqual(record.autonomous_system_number, 1221)
86+
self.assertEqual(record.autonomous_system_organization,
87+
'Telstra Pty Ltd')
88+
self.assertEqual(record.ip_address, ip_address)
89+
90+
self.assertRegex(
91+
str(record),
92+
r'geoip2.models.ASN\(.*1\.128\.0\.0.*\)',
93+
'str representation is correct')
94+
95+
self.assertEqual(record, eval(repr(record)), "ASN repr can be eval'd")
96+
97+
reader.close()
98+
7999
def test_city(self):
80100
reader = geoip2.database.Reader(
81101
'tests/data/test-data/GeoIP2-City-Test.mmdb')

0 commit comments

Comments
 (0)