Skip to content

Instantly share code, notes, and snippets.

@void285
Created June 8, 2019 05:32
Show Gist options
  • Save void285/c383b10550495d8e332089d0c8a6317a to your computer and use it in GitHub Desktop.
Save void285/c383b10550495d8e332089d0c8a6317a to your computer and use it in GitHub Desktop.
Refresh/Clear cloudflare site cache with your API. This is the python version. See more at https://vps123.top/refresh-cloudflare-site-files-cache.html
#coding: utf-8
import requests
import argparse
import json
# your account email
EMAIL = 'user@example.com'
# your account api, get from the profile page
APIKEY = 'c2547eb745079dac9320b638f5e225cf483cc5cfdda41'
# zoneids of your sites
SITES = {
'sitea': '31a8372d0c353023e105f4ecef8ad9ca',
'siteb': '023e105f4ecef8ad9ca31a8372d0c353',
}
def main(siteid, files):
zone = SITES[siteid]
url = "https://api.cloudflare.com/client/v4/zones/%s/purge_cache" % (zone)
headers = {
'X-Auth-Email': EMAIL,
'X-Auth-Key': APIKEY,
'Content-Type': 'application/json'
}
res = requests.post(url, headers=headers, data='%s' % json.dumps({"files": files}), timeout=10)
if res.status_code != 200:
print(res.status_code)
print(res.text)
if __name__ == '__main__':
argParser = argparse.ArgumentParser()
argParser.add_argument('-s', '--site', dest='site', type=str, help='site', required=True, choices=SITES.keys())
argParser.add_argument('-f', '--files', dest='files', type=str, help='files', required=True)
args = argParser.parse_args()
# pass the urls with | separator
main(args.site, args.files.split("|"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment