Skip to content

Instantly share code, notes, and snippets.

@Nusab19
Last active September 27, 2025 13:05
Show Gist options
  • Select an option

  • Save Nusab19/e166b7eed3abba17d901274d58ffa89b to your computer and use it in GitHub Desktop.

Select an option

Save Nusab19/e166b7eed3abba17d901274d58ffa89b to your computer and use it in GitHub Desktop.
A fun way to get your ip address using Cloudflare
import requests, re
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
urls = [
"http://172.66.44.241/", # cloudflare's ip
"https://a211a49a8bb35.pages.dev/", # suspended phishing site
]
pattern = re.compile(r'<span[^>]*id="cf-footer-ip"[^>]*>(.*?)</span>', re.I)
def getIP(ipv4Only=False, noPrint=True):
"""
Retrieves public IP addresses (IPv4 and IPv6) by making requests to specified URLs.
Args:
ipv4Only (bool): If True, only returns IPv4 address. Defaults to False.
noPrint (bool): If True, suppresses console output. Defaults to True.
Returns:
dict: Dictionary containing 'ipv4' and 'ipv6' keys with respective IP addresses.
IPv6 will be None if it's the same as IPv4 or if ipv4Only is True.
"""
ses = requests.Session()
r1 = ses.get(urls[0], verify=False)
ipv4 = pattern.search(r1.text)
ipv4 = ipv4.group(1).strip() if ipv4 else None
if ipv4Only:
return {
"ipv4": ipv4,
}
if not noPrint:
print(f"IPv4: {ipv4}")
r2 = ses.get(urls[1], verify=False)
ipv6 = pattern.search(r2.text)
ipv6 = ipv6.group(1).strip() if ipv6 else None
if ipv4 != ipv6 and not noPrint:
print("IPv6:", ipv6)
return {
"ipv4": ipv4,
"ipv6": ipv6 if ipv4 != ipv6 else None,
}
if __name__ == "__main__":
res = getIP()
print(res) # {'ipv4': '104.28.240.84', 'ipv6': '2a09:bac1:b00:10::1f1:1d9'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment