Created
December 22, 2022 02:10
-
-
Save dhoeric/7681aa9fb857430a3b6c038aecb51bfb to your computer and use it in GitHub Desktop.
Check if IP address belongs to Cloudflare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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