我尝试使用Python GeoIP使用Maxmind数据库将IP地址转换为Geo详细信息。
pipeline script from SCM
我收到错误“ geoip2.errors.AddressNotFoundError:地址103.229.234.197不在数据库中。”
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
Maxmind db提到的地址不在数据库中。然而它不会伤害我但是如何忽略这个错误并得到我的输出哪个可用呢?
尝试除了任何错误(虽然不是最好的方法),也期望特定的AddressNotFoundError。
Traceback (most recent call last):
File "script.py", line 14, in <module>
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.
也
try:
print line.strip(), response.country.name, response.country.iso_code, response.location.longitude, response.location.latitude
except:
pass
仍然没有运气。
任何建议。
答案 0 :(得分:2)
这里的问题是db.city调用中发生的异常,而不是值打印,所以你可以试试这个:
5.0.beta1