1 Star 0 Fork 1

爱玲 / IpApi_by_ip2Region

Create your Gitee Account
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
Clone or download
Ip2RegionApi.py 1.20 KB
Copy Edit Web IDE Raw Blame History
一朵灼灼华 authored 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'])

Comment ( 0 )

Sign in for post a comment

Python
1
https://gitee.com/xiaoshen2017/IpApi_by_ip2Region.git
git@gitee.com:xiaoshen2017/IpApi_by_ip2Region.git
xiaoshen2017
IpApi_by_ip2Region
IpApi_by_ip2Region
master

Search