Reputation: 37
I'm using Django 1.73 on Python 2.7.
How can I query the ASN for a known IP address using GeoIP Django?
from django.contrib.gis.geoip import GeoIP
g = GeoIP()
g.city('72.14.207.99') #returns the city
g.country('google.com') #returns the country
how can I query the asn with Django? (have installed the ASN binaries on GEO_PATH)
Edit
I've researched and found there exist a Python package PyGeoIP,
In documentation there exist an ASN lookup but I don't know how to call with GeoDjango directly than using this package.
Upvotes: 0
Views: 2498
Reputation: 131
If you are happy to use pygeoIp it is pretty straightforward.
First download the ASN database from GeoIP then install pygeoip
by
pip install pygeoip
then
>>> gi = pygeoip.GeoIP('/path/to/GeoIPASNum.dat')
>>> gi.org_by_name('cnn.com')
'AS5662 Turner Broadcasting'
or
>>> gi.org_by_addr("90.223.167.43")
u'AS5607 BSKYB-BROADBAND-AS'
If you do not want to download any database you can use pyip2asn
In [1]:import pyip2asn
In [2]:# Generate ASN Description Dictionary in order to speed up the IPv4 to ASN lookup.
In [3]: asn_dict = pyip2asn.get_asn_dictionary()
In [4]: pyip2asn.ip2asn("8.8.8.8",asn_dict)
Out[4]: (15169, u'GOOGLE - Google Inc.')
Upvotes: 3