mirror of
https://github.com/lionsoul2014/ip2region.git
synced 2025-12-08 19:25:22 +00:00
2.增加search_test 3.修改ip2Region.py及类名,改为xdbSearcher Signed-off-by: 厉害的花花 <117415792@qq.com>
52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
|
|
from xdbSearcher import XdbSearcher
|
|
|
|
def searchWithFile():
|
|
# 1. 创建查询对象
|
|
dbPath = "../../data/ip2region.xdb"
|
|
searcher = XdbSearcher(dbfile=dbPath)
|
|
|
|
# 2. 执行查询
|
|
ip = "1.2.3.4"
|
|
region_str = searcher.searchByIPStr(ip)
|
|
print(region_str)
|
|
|
|
# 3. 关闭searcher
|
|
searcher.close()
|
|
|
|
def searchWithVectorIndex():
|
|
# 1. 预先加载整个 xdb
|
|
dbPath = "../../data/ip2region.xdb"
|
|
vi = XdbSearcher.loadVectorIndexFromFile(dbfile=dbPath)
|
|
|
|
# 2. 使用上面的缓存创建查询对象, 同时也要加载 xdb 文件
|
|
searcher = XdbSearcher(dbfile=dbPath, vectorIndex=vi)
|
|
|
|
# 3. 执行查询
|
|
ip = "1.2.3.4"
|
|
region_str = searcher.search(ip)
|
|
print(region_str)
|
|
|
|
# 4. 关闭searcher
|
|
searcher.close()
|
|
|
|
def searchWithContent():
|
|
# 1. 预先加载整个 xdb
|
|
dbPath = "../../data/ip2region.xdb";
|
|
cb = XdbSearcher.loadContentFromFile(dbfile=dbPath)
|
|
|
|
# 2. 仅需要使用上面的全文件缓存创建查询对象, 不需要传源 xdb 文件
|
|
searcher = XdbSearcher(contentBuff=cb)
|
|
|
|
# 3. 执行查询
|
|
ip = "1.2.3.4"
|
|
region_str = searcher.search(ip)
|
|
print(region_str)
|
|
|
|
# 4. 关闭searcher
|
|
searcher.close()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
searchWithContent()
|
|
|