Skip to content

Instantly share code, notes, and snippets.

@dhoeric
Created December 22, 2022 02:10
Show Gist options
  • Save dhoeric/7681aa9fb857430a3b6c038aecb51bfb to your computer and use it in GitHub Desktop.
Save dhoeric/7681aa9fb857430a3b6c038aecb51bfb to your computer and use it in GitHub Desktop.
Check if IP address belongs to Cloudflare
import requests
import ipaddress
# Get Cloudflare IP Ranges
# ref: https://www.cloudflare.com/ips/
def get_cf_ips():
ips = []
for urls in [
"https://www.cloudflare.com/ips-v4",
"https://www.cloudflare.com/ips-v6"
]:
res = requests.get(urls)
for ip in res.text.split("\n"):
ips.append(ipaddress.ip_network(ip))
return ips
ip = "8.8.8.8" # just an example
cf_ips = get_cf_ips()
# True if "ip" belongs to Cloudflare
print(any([ipaddress.ip_address(ip) in cf_ip for cf_ip in cf_ips()]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment