1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import json import requests from datetime import date
file = open("ip.txt", "r") email = "your cloudflare mail" apikey = "your cloudflare apikey"
url = "https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules" mode = "block"
for line in file: ip = line.strip() try: r = requests.post(url, headers = {'X-Auth-Email': email, 'X-Auth-Key': apikey, 'Content-Type': 'application/json'}, json = {"mode": mode,"configuration": {"target": "ip", "value": ip}, "notes": date.today().strftime("%Y-%m-%d")}) r = json.loads(r.text) if r["success"]: print(ip, "添加成功") else: print(ip, "添加失败", r["errors"][0]["message"]) except Exception as e: print(ip, "添加失败", e)
|