python绝技Geolite2-city.mmd 使用方法。“Invalid database type, expected City”

一在使用python绝技中的代码时,下载的Geolite是.dat文件

import pygeoip




def PrintRecord(tgt):
    try:
        gi = pygeoip.GeoIP('./GeoLiteCity/GeoLiteCity.mmdb')
        rec = gi.record_by_name(tgt)
        city = rec['city']
        print(city)
    except Exception as e:
        print(e)
        return



if __name__ == '__main__':
    tgt = '220.168.56.116'
    PrintRecord(tgt)

报错“Invalid database type, expected City”

二、现在下载的是Geolite2-city.mmdb文件格式不一样

下载网址:https://dev.maxmind.com/geoip/geoip2/geolite2/

import geoip2.database
import sys

ip = '172.104.68.126'

reader = geoip2.database.Reader('./GeoLiteCity/GeoLiteCity.mmdb')

data = reader.city(ip)

print("IP Address: ", ip)
print("Country:", data.country.name)
print("Subdivisions: ", data.subdivisions.most_specific.name)
print("City: ", data.city.name)
print("Latitude: ", data.location.latitude)
print("Longitude: ", data.location.longitude)

 

输出:

IP Address:  172.104.68.126
Country: Japan
Subdivisions:  Tokyo
City:  Tokyo
Latitude:  35.6882
Longitude:  139.7532
 

 

 

发布了19 篇原创文章 · 获赞 1 · 访问量 2036
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览