1 Star 1 Fork 1

一朵灼灼华 / IpApi_by_ip2Region

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Ip2RegionApi.py 1.20 KB
一键复制 编辑 原始数据 按行查看 历史
一朵灼灼华 提交于 2019-09-26 13:37 . 更新数据库
#! python3
# -*- coding:utf-8 -*-
"""
" ip2region python api test
"
" Autho: Heanny<lzh@heanny.cn>
" Date : 2019-01-01
"""
import time
from python.ip2Region import Ip2Region
def GetIpRegion(ips, algorithm="b-tree"):
'''三种查询方法'''
algorithms = ["binary", "b-tree", "memory"]
try:
algorithms.index(algorithm)
except Exception as e:
algorithm = "b-tree"
dbFile = "./data/ip2region.db"
searcher = Ip2Region(dbFile)
ReData = {}
for ip in ips:
try:
sTime = time.time() * 1000
if algorithm == "binary":
data = searcher.binarySearch(ip)
elif algorithm == "memory":
data = searcher.memorySearch(ip)
else:
data = searcher.btreeSearch(ip)
eTime = time.time() * 1000
data['time'] = eTime - sTime
ReData[ip] = data
print("%s|%s in %5f ms" % (data["city_id"], data["region"].decode('utf-8'), eTime - sTime))
except Exception as e:
print("[Error]: %s" % e)
ReData[ip] = {}
searcher.close()
return ReData
if __name__ == '__main__':
GetIpRegion(['183.131.1.101', '2.2.2.2', '3.3.3.3'])
Python
1
https://gitee.com/heanny/IpApi_by_ip2Region.git
git@gitee.com:heanny/IpApi_by_ip2Region.git
heanny
IpApi_by_ip2Region
IpApi_by_ip2Region
master

搜索帮助