Python geoip2模块关联ip地址和物理位置(经纬度)
注意:在运行代码之前,需要先下载GeoLite2-City.mmdb这个配套的数据库,如果下载有问题的,可以直接去我的资源里下载,我已将资源上传
import geoip2.database
reader = geoip2.database.Reader(
'GeoLite2-City.mmdb')
def ip_print_AddrInfo(ip):
response = reader.city(ip)
Country_IsoCode = response.country.iso_code
Country_Name = response.country.name
Country_NameCN = response.country.names['zh-CN']
City_PostalCode = response.postal.code
Location_Latitude = response.location.latitude
Location_Longitude = response.location.longitude
print('[*] Target: ' + ip + ' GeoLite2-Located ')
print(' [+] Country_IsoCode : ' + Country_IsoCode)
print(' [+] Country_Name : ' + Country_Name)
print(' [+] Country_NameCN : ' + Country_NameCN)
if City_PostalCode is not None:
print(' [+] City_PostalCode : ' + City_PostalCode)
print(' [+] Location_Latitude : ' + str(Location_Latitude))
print(' [+] Location_Longitude : ' + str(Location_Longitude))
ip = '218.58.101.229' //将ip地址改为你想要查找的地址,这里我用的是苹果网站的地址
ip_print_AddrInfo(ip)