To determine the Autonomous System (AS) CIDR for an IP address using IP2Location.io in Python, you can use the IP2Location.io API to retrieve geolocation and related data.
python -m pip install requests
import requests
from requests.exceptions import HTTPError
import json
key = 'YOUR_API_KEY'
ip = '8.8.8.8'
payload = {'key': key, 'ip': ip, 'format': 'json'}
try:
response = requests.get('https://api.ip2location.io/', params=payload)
response.raise_for_status()
except HTTPError as http_err:
if 'error_message' in response.text:
myobj = json.loads(response.text)
print(f"ERROR: {myobj['error']['error_message']}")
else:
print(f"ERROR: {http_err}")
except Exception as err:
print(f"ERROR: {err}")
else:
try:
myobj = json.loads(response.text)
if 'as_info' in myobj:
print(f"The AS CIDR for IP {ip} is {myobj['as_info']['as_cidr']}.")
else:
print("ERROR: The as_cidr field requires a paid subscription to the Security plan.")
except json.JSONDecodeError as e:
print("ERROR: Invalid JSON in response.")
python test.py
This script will output the AS CIDR of specified IP address. Make sure to replace 8.8.8.8 with the IP address you want and replace YOUR_API_KEY to your own API key.
Empower your applications with accurate IP geolocation information now.
Try It for Free