from ipaddress import ip_network import json import requests def get_country_by_ip(ip): response = requests.get("http://ip-api.com/json/" + ip) res = json.loads(response.text) return res.get("country") if __name__ == "__main__": ip = input("请输入IP地址 :") print("此IP所属国家为:" + get_country_by_ip(ip)) #请输入IP地址 :133.15.78.15 #此IP所属国家为:Japan