Python GeoIP2 AddressNotFoundError
<p>Im trying to use Python GeoIP to convert IP address to Geo details using Maxmind Database.</p>

<pre><code>import urllib2
import csv
import geoip2.database
db = geoip2.database.Reader("GeoLite2-City.mmdb")
target_url="http://myip/all.txt"
data = urllib2.urlopen(target_url)
for line in data:
        response = db.city(line.strip())
        print line.strip(), response.country.name, response.country.iso_code, response.location.longitude, response.location.latitude
</code></pre>

<p>Im getting the error "<strong>geoip2.errors.AddressNotFoundError:</strong> The address 103.229.234.197 is not in the database."</p>

<pre><code>    Traceback (most recent call last):
  File "script.py", line 14, in &lt;module&gt;
    response = db.city(line.strip())
  File "/usr/lib/python2.7/site-packages/geoip2/database.py", line 110, in city
    return self._model_for(geoip2.models.City, 'City', ip_address)
  File "/usr/lib/python2.7/site-packages/geoip2/database.py", line 180, in _model_for
    record = self._get(types, ip_address)
  File "/usr/lib/python2.7/site-packages/geoip2/database.py", line 176, in _get
    "The address %s is not in the database." % ip_address)
geoip2.errors.AddressNotFoundError: The address 103.229.234.197 is not in the database.
</code></pre>

<p>Maxmind db mentions as the address not in database. However its not going to hurt me but how can a ignore this error and get my output of which ever is available?</p>

<p>Tried to except any error (although not a best way) and also to expect particular AddressNotFoundError. </p>

<pre><code>try:
     print line.strip(), response.country.name, response.country.iso_code, response.location.longitude, response.location.latitude
except:
       pass
</code></pre>

<p>also,</p>

<pre><code>try:
     print line.strip(), response.country.name, response.country.iso_code, response.location.longitude, response.location.latitude

except AddressNotFoundError:
     pass
</code></pre>

<p>Still no luck.</p>

<p>Any suggestions.</p>

<p>The problem here is the exception happening in db.city call, not in values printing, so you could try this:</p>

<pre><code>import urllib2
import csv
import geoip2.database
db = geoip2.database.Reader("GeoLite2-City.mmdb")
target_url="http://myip/all.txt"
data = urllib2.urlopen(target_url)
for line in data:
    try:
        response = db.city(line.strip())
        print line.strip(), response.country.name, response.country.iso_code, response.location.longitude,   response.location.latitude
    exception:
        pass
</code></pre>

python exception exception-handling geoip maxmind