需求
获取Cloudflare的所有节点IP,根据CF公开的IP范围(https://www.cloudflare.com/ips-v4)
依赖库
IPy库
1 | pip install IPy |
敲
借鉴(实际上是照抄)https://blog.csdn.net/grey_csdn/article/details/70195393:
1 2 3 4 5 6 7 8 9 | from IPy import IP ip = IP('104.16.0.0/12') print(ip.len()) for x in ip: print(x) |
还有就是要从Cloudflare官网下载IP列表
获取IP列表可以直接用requests.get来办,这里不多介绍
整理一下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | from IPy import IP import requests cfIp_Response = requests.get("https://www.cloudflare.com/ips-v4", headers={ 'user-agent': 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.7113.93 Safari/537.36'}) ipList = cfIp_Response.text.split("\n") def output(ips): ip = IP(ips) print(ip.len()) for x in ip: print(x) for ipGroup in ipList: output(ipGroup) |
既然是IP,那不仅有IPv4,还有IPv6
如果有需要的话可以直接简单粗暴地把get的URL里面的v4改成v6
发表回复