代码拉取完成,页面将自动刷新
#! 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'])
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。