可以去研究下,顺便贴下我之前用来查询 geoip 数据库的,给你参考参考:
python-query-ip
import geoip2.database
def query_ip_info(ip):
# 打开ASN数据库并查询
with geoip2.database.Reader('./GeoLite2-ASN.mmdb') as reader:
response = reader.asn(ip)
asn_number = response.autonomous_system_number
isp = response.autonomous_system_organization
# 打开City数据库并查询
with geoip2.database.Reader('./GeoLite2-City.mmdb') as reader:
response = reader.city(ip)
country = response.country.name
province = response.subdivisions.most_specific.name
city = response.city.name
postal_code = response.postal.code
latitude = response.location.latitude
longitude = response.location.longitude
# 打开Country数据库并查询
with geoip2.database.Reader('./GeoLite2-Country.mmdb') as reader:
response = reader.country(ip)
country = response.country.name
return {
'asn_number': asn_number,
'isp': isp,
'country': country,
'province': province,
'city': city,
'postal_code': postal_code,
'latitude': latitude,
'longitude': longitude
}
query_ip = input("输入查询IP:")
query_ip = query_ip.strip()
if query_ip:
# 使用函数查询IP信息
ip_info = query_ip_info(query_ip)
# 打印查询结果
for key, value in ip_info.items():
print(f'{key}: {value}')
else:
print("未提供有效IP")