网站操作可行性分析如何网络推广
简介:ip2region是一个离线IP地址定位库和IP定位数据管理框架,10微秒级别的查询效率,提供了众多主流编程语言的 xdb 数据生成和查询客户端实现。号称准确率99.9%的开源离线IP地址定位库。
历史攻略:
Python:暴力破解密码 - 压缩包、web实战
Python:暴力破解密码
真伪之眼:探索信息纷争的艺术
Python:浏览网页暴露多少信息?
GitHub地址:
https://github.com/lionsoul2014/ip2region
使用步骤:
1、下载源码和数据
https://github.com/lionsoul2014/ip2region/archive/refs/heads/master.zip
2、本质就是导入和使用data目录下的 ip2region.xdb
官方示例代码:iptest.py
# Copyright 2022 The Ip2Region Authors. All rights reserved.
# Use of this source code is governed by a Apache2.0-style
# license that can be found in the LICENSE file.
#from xdbSearcher import XdbSearcherdef searchWithFile():# 1. 创建查询对象dbPath = "ip2region.xdb"searcher = XdbSearcher(dbfile=dbPath)# 2. 执行查询ip = "1.2.3.4"region_str = searcher.searchByIPStr(ip)print(region_str)# 3. 关闭searchersearcher.close()def searchWithVectorIndex():# 1. 预先加载整个 xdbdbPath = "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. 关闭searchersearcher.close()def searchWithContent():# 1. 预先加载整个 xdbdbPath = "ip2region.xdb"cb = XdbSearcher.loadContentFromFile(dbfile=dbPath)# 2. 仅需要使用上面的全文件缓存创建查询对象, 不需要传源 xdb 文件searcher = XdbSearcher(contentBuff=cb)# 3. 执行查询# ip = "111.112.113.114"ip = "101.102.103.104"region_str = searcher.search(ip)print(region_str)# 4. 关闭searchersearcher.close()if __name__ == '__main__':searchWithContent()
运行结果:
中国|0|宁夏|固原市|电信
中国|0|台湾省|台北|0