教你查别人ip地址精确位置(通过ip查地址的网站)

在大数据时代,了解收集用户来源是必不可少的功能之一,本地搭建IP库进行IP分析是ELK日志分析功能的一部分,本文进行了工具拆解,将一步步教你搭建日志分析平台。

教你查别人ip地址精确位置(通过ip查地址的网站)

安装 geoip2 库

pip install geoip2 -i https://pypi.douban.com/simple

下载 Maxmind 网站的 IP 信息库

地址:http://dev.maxmind.com/geoip/geoip2/geolite2

下载完之后,解压,将解压包中的 mmdb 文件复制到项目的应用程序中

本机使用的环境是python3.7.4

直接上代码

#!/usr/bin/python# -*-coding:utf-8-*-import geoip2.databasereader = geoip2.database.Reader('./GeoLite2-City.mmdb')# ip = input("输入你要查询的IP:n")response = reader.city('123.58.180.8') #网易ping www.163.com# response = reader.city(ip)# 有多种语言,我们这里主要输出英文和中文print(response.city.name)print("你查询的IP的地理位置是:")print("地区:{}({})".format(response.continent.names["es"], response.continent.names["zh-CN"]))print("国家:{}({}) ,简称:{}".format(response.country.name, response.country.names["zh-CN"], response.country.iso_code))print("洲/省:{}({})".format(response.subdivisions.most_specific.name, response.subdivisions.most_specific.names["zh-CN"]))print("城市:{}({})".format(response.city.name, response.city.names["zh-CN"]))print("经度:{},纬度{}".format(response.location.longitude, response.location.latitude))print("时区:{}".format(response.location.time_zone))print("邮编:{}".format(response.postal.code))

打印出结果

你查询的IP的地理位置是:地区:Asia(亚洲)国家:China(中国) ,简称:CN洲/省:Zhejiang(浙江省)城市:Hangzhou(杭州)经度:120.1619,纬度30.294时区:Asia/Shanghai邮编:None

2.至于在线库的使用是需要一个license_key

只有前面两步不太一样

连接: client = geoip2.webservice.Client(42, <license_key>)

查询 IP: response = client.insights(<IP>)

(0)
小多多的头像小多多创始人

相关推荐

发表回复

登录后才能评论