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
 

 

 

你可能感兴趣的:(语言)