def get_ip_from_url(url):
try:
response = requests.get(fhttp://{url})
ip_address = response.headers['XForwardedFor'] or response.headers['ProxyClientIP'] or response.headers['WLProxyClientIP'] or response.headers['HTTP_X_FORWARDED_FOR'] or response.headers['HTTP_X_REAL_IP'] or response.headers['HTTP_CLIENT_IP'] or response.headers['HTTP_X_FORWARDED_HOST'] or response.headers['HTTP_X_CLUSTER_CLIENT_IP'] or response.headers['HTTP_FORWARDED_FOR'] or response.headers['HTTP_FORWARDED'] or response.headers['REMOTE_ADDR']
return ip_address
except Exception as e:
print(fAn error occurred: {e})
return None
示例用法
url = example.com
ip_address = get_ip_from_url(url)
print(fThe IP address for {url} is: {ip_address})