龙空技术网

Python查IP归属地

冬瓜视频1号 92

前言:

如今咱们对“pythonip地址查询”大体比较关心,兄弟们都需要知道一些“pythonip地址查询”的相关文章。那么小编在网络上网罗了一些有关“pythonip地址查询””的相关知识,希望你们能喜欢,各位老铁们一起来学习一下吧!

import requests

def get_ip_location(ip):

try:

# 使用免费的ipinfo.io API

response = requests.get(f"{ip}/json")

data = response.json()

return data.get("city", "Unknown"), data.get("region", "Unknown"), data.get("country", "Unknown")

except:

return "Unknown", "Unknown", "Unknown"

# IP列表

ips = [

"162.14.17.179",

"43.129.254.179",

"162.14.18.121",

"14.152.91.163",

"119.29.29.98",

"43.154.252.226",

"175.99.212.48",

"175.99.212.74",

"175.97.167.112",

"113.240.72.111",

"183.47.110.249",

"43.135.105.128",

"175.99.23.57",

"129.226.1.69",

"121.14.77.149",

"183.47.111.104"

]

for ip in ips:

city, region, country = get_ip_location(ip)

#print(f"IP: {ip}, City: {city}, Region: {region}, Country: {country}")

print(f"IP: {ip}, Country: {country}")

标签: #pythonip地址查询