Skip to content

xdbsearher

python
# https://gitee.com/lionsoul/ip2region/blob/master/data/ip2region.xdb
# https://gitee.com/lionsoul/ip2region/blob/master/binding/python/xdbSearcher.py

import os
from xdbSearcher import XdbSearcher


class GeoIP:

    def __init__(self) -> None:
        dbfile = os.path.join(os.path.dirname(__file__), 'ip2region.xdb')
        contentBuff = XdbSearcher.loadContentFromFile(dbfile=dbfile)
        self.searcher = XdbSearcher(contentBuff=contentBuff)

    def get_location(self, ip: str) -> str:
        try:
            return self.searcher.search(ip)
        except:
            return ''

geo_ip = GeoIP()
print(geo_ip.get_location('1.1.1.1')) # 澳大利亚|0|0|0|0