当前位置: 首页 > 工具软件 > open-geoip > 使用案例 >

geoip2 InvalidDatabaseError: Invalid node in search tree

牟飞沉
2023-12-01

问题

在使用geoip2模块去解析ip地址是出现了错误:InvalidDatabaseError: Invalid node in search tree

(venv) [hilqiqi0@izbp1a3rsiacm6nu3bfnyuz ~]$ ipython
Python 3.6.8 (default, Jan 17 2019, 16:30:37)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.5.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import geoip2.database

In [2]: reader = geoip2.database.Reader('GeoLite2-City.mmdb')

In [3]: reader.city("117.136.81.221")
---------------------------------------------------------------------------
InvalidDatabaseError                      Traceback (most recent call last)
<ipython-input-3-d3607d6e2613> in <module>
----> 1 reader.city("117.136.81.221")

/data/hilqiqi0/venv/lib/python3.6/site-packages/geoip2/database.py in city(self, ip_address)
    112
    113         """
--> 114         return self._model_for(geoip2.models.City, 'City', ip_address)
    115
    116     def anonymous_ip(self, ip_address):

/data/hilqiqi0/venv/lib/python3.6/site-packages/geoip2/database.py in _model_for(self, model_class, types, ip_address)
    193
    194     def _model_for(self, model_class, types, ip_address):
--> 195         record = self._get(types, ip_address)
    196         record.setdefault('traits', {})['ip_address'] = ip_address
    197         return model_class(record, locales=self._locales)

/data/hilqiqi0/venv/lib/python3.6/site-packages/geoip2/database.py in _get(self, database_type, ip_address)
    186                             "%s database" % (caller,
    187                                              self.metadata().database_type))
--> 188         record = self._db_reader.get(ip_address)
    189         if record is None:
    190             raise geoip2.errors.AddressNotFoundError(

/data/hilqiqi0/venv/lib/python3.6/site-packages/maxminddb/reader.py in get(self, ip_address)
    114                 'Error looking up {0}. You attempted to look up '
    115                 'an IPv6 address in an IPv4-only database.'.format(ip_address))
--> 116         pointer = self._find_address_in_tree(address)
    117
    118         return self._resolve_data_pointer(pointer) if pointer else None

/data/hilqiqi0/venv/lib/python3.6/site-packages/maxminddb/reader.py in _find_address_in_tree(self, ip_address)
    135             return node
    136
--> 137         raise InvalidDatabaseError('Invalid node in search tree')
    138
    139     def _start_node(self, length):

InvalidDatabaseError: Invalid node in search tree

In [4]: quit()

问题分析

由于之前使用的程序没有问题,在本机电脑上运行也正常,换了台服务器就蹦了;从最开始找代码问题,到怀疑模块版本和python版本不匹配,然后到怀疑python基础代码有问题,最后重新下载文件、重新解压才正常。
必须用gunzip进行解压(https://github.com/maxmind/MaxMind-DB-Reader-python );文件地址(https://dev.maxmind.com/geoip/geoip2/geolite2/


(venv) [hilqiqi0@izbp1a3rsiacm6nu3bfnyuz geoip]$ wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz
--2019-05-14 10:33:41--  https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz
Resolving geolite.maxmind.com (geolite.maxmind.com)... 104.17.201.89, 104.17.200.89, 2606:4700::6811:c959, ...
Connecting to geolite.maxmind.com (geolite.maxmind.com)|104.17.201.89|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 29204309 (28M) [application/gzip]
Saving to: ‘GeoLite2-City.tar.gz’

100%[=======================================================================================>] 29,204,309  8.90MB/s   in 3.1s

2019-05-14 10:33:45 (8.90 MB/s) - ‘GeoLite2-City.tar.gz’ saved [29204309/29204309]

(venv) [hilqiqi0@izbp1a3rsiacm6nu3bfnyuz geoip]$ ls
GeoLite2-City.tar.gz
(venv) [hilqiqi0@izbp1a3rsiacm6nu3bfnyuz geoip]$ gunzip GeoLite2-City.tar.gz
(venv) [hilqiqi0@izbp1a3rsiacm6nu3bfnyuz geoip]$ ls
GeoLite2-City.tar
(venv) [hilqiqi0@izbp1a3rsiacm6nu3bfnyuz geoip]$ tar -xvf GeoLite2-City.tar
GeoLite2-City_20190514/
GeoLite2-City_20190514/COPYRIGHT.txt
GeoLite2-City_20190514/README.txt
GeoLite2-City_20190514/LICENSE.txt
GeoLite2-City_20190514/GeoLite2-City.mmdb
(venv) [hilqiqi0@izbp1a3rsiacm6nu3bfnyuz geoip]$ ls
GeoLite2-City_20190514  GeoLite2-City.tar
(venv) [hilqiqi0@izbp1a3rsiacm6nu3bfnyuz geoip]$ cd GeoLite2-City_20190514/
(venv) [hilqiqi0@izbp1a3rsiacm6nu3bfnyuz GeoLite2-City_20190514]$ ls
COPYRIGHT.txt  GeoLite2-City.mmdb  LICENSE.txt  README.txt
(venv) [hilqiqi0@izbp1a3rsiacm6nu3bfnyuz GeoLite2-City_20190514]$ ipython
Python 3.6.8 (default, Jan 17 2019, 16:30:37)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.5.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import geoip2.database

In [2]: reader = geoip2.database.Reader('GeoLite2-City.mmdb')

In [3]: reader.city("117.136.81.221")
Out[3]: geoip2.models.City({'city': {'geoname_id': 1791247, 'names': {'de': 'Wuhan', 'en': 'Wuhan', 'es': 'Wuhan', 'fr': 'Wuhan', 'ja': '武漢市', 'pt-BR': 'Wuhan', 'ru': 'Ухань', 'zh-CN': '武汉'}}, 'continent': {'code': 'AS', 'geoname_id': 6255147, 'names': {'de': 'Asien', 'en': 'Asia', 'es': 'Asia', 'fr': 'Asie', 'ja': 'アジア', 'pt-BR': 'Ásia', 'ru': 'Азия', 'zh-CN': '亚洲'}}, 'country': {'geoname_id': 1814991, 'iso_code': 'CN', 'names': {'de': 'China', 'en': 'China', 'es': 'China', 'fr': 'Chine', 'ja': '中国', 'pt-BR': 'China', 'ru': 'Китай', 'zh-CN': '中国'}}, 'location': {'accuracy_radius': 200, 'latitude': 30.5856, 'longitude': 114.2665, 'time_zone': 'Asia/Shanghai'}, 'registered_country': {'geoname_id': 1814991, 'iso_code': 'CN', 'names': {'de': 'China', 'en': 'China', 'es': 'China', 'fr': 'Chine', 'ja': '中国', 'pt-BR': 'China', 'ru': 'Китай', 'zh-CN': '中国'}}, 'subdivisions': [{'geoname_id': 1806949, 'iso_code': 'HB', 'names': {'de': 'Hubei', 'en': 'Hubei', 'es': 'Hubei', 'fr': 'Province de Hubei', 'ja': '湖北省', 'pt-BR': 'Hubei', 'ru': 'Хубэй', 'zh-CN': '湖北省'}}], 'traits': {'ip_address': '117.136.81.221'}}, ['en'])

In [4]:  
 类似资料:

相关阅读

相关文章

相关问答