commit 2627cd5ad2058d616cc037cd590b9f64112882a9 Author: Arturo Filastò arturo@filasto.net Date: Mon May 2 17:54:36 2016 +0200
Make the geoip lookup code more robust --- ooni/geoip.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)
diff --git a/ooni/geoip.py b/ooni/geoip.py index dcc799e..fa6d1ae 100644 --- a/ooni/geoip.py +++ b/ooni/geoip.py @@ -34,25 +34,22 @@ def IPToLocation(ipaddr): asn_file = config.get_data_file_path('GeoIP/GeoIPASNum.dat')
location = {'city': None, 'countrycode': 'ZZ', 'asn': 'AS0'} - - def error(): + if not asn_file or not country_file: log.err("Could not find GeoIP data file in data directories." "Try running ooniresources or" " edit your ooniprobe.conf") + return location
- try: - country_dat = GeoIP(country_file) - location['countrycode'] = country_dat.country_code_by_addr(ipaddr) - if not location['countrycode']: - location['countrycode'] = 'ZZ' - except IOError: - error() + country_dat = GeoIP(country_file) + asn_dat = GeoIP(asn_file)
- try: - asn_dat = GeoIP(asn_file) - location['asn'] = asn_dat.org_by_addr(ipaddr).split(' ')[0] - except: - error() + country_code = country_dat.country_code_by_addr(ipaddr) + if country_code is not None: + location['countrycode'] = country_code + + asn = asn_dat.org_by_addr(ipaddr) + if asn is not None: + location['asn'] = asn.split(' ')[0]
return location
tor-commits@lists.torproject.org