代码如下:
import requests
from netaddr import IPNetwork, IPAddress
import json
class AWSIPInfo:
def __init__(self, ip):
self.ip = ip
self.url = 'https://ip-ranges.amazonaws.com/ip-ranges.json'
try:
self.aws_ips = json.loads(self._load_aws_ips())
except Exception as e:
raise Exception("Error loading AWS IP addresses: " + str(e))
def _load_aws_ips(self):
try:
response = requests.get(self.url, allow_redirects=True, verify=False)
response.raise_for_status()
return response.content
except Exception as e:
raise Exception("Error loading AWS IP addresses: " + str(e))
def get_domain_name(self):
for item in self.aws_ips["prefixes"]:
if IPAddress(self.ip) in IPNetwork(str(item["ip_prefix"])):
return str(item["region"]) + ".compute.amazonaws.com"
# 没有找到匹配项时,返回提示信息
return "No matching IP address found in AWS IP ranges."