Check Cloudflare Cache Status

Check if a resource is served from Cloudflare's cache by inspecting the CF-Cache-Status header.

Code

General
import urllib.request
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
with urllib.request.urlopen(req) as response:
    return response.headers.get('CF-Cache-Status', 'No CF-Cache-Status header')

Parameters

URL to check

Server

CF-Cache-Status Values

StatusMeaning
HITServed from Cloudflare cache
MISSNot in cache, fetched from origin
EXPIREDWas cached but has expired, refetched
STALEServed stale while revalidating
DYNAMICNot eligible for caching (e.g., POST requests, dynamic content)
BYPASSCache bypassed due to configuration

Real-World Usage

import urllib.request

def check_cf_cache(url):
    req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
    with urllib.request.urlopen(req) as response:
        status = response.headers.get('CF-Cache-Status')
        age = response.headers.get('Age')
        return {
            'cached': status == 'HIT',
            'status': status,
            'age': int(age) if age else None
        }

Testing with httpbin

Use httpbin to simulate different cache statuses:

  • https://httpbin.org/response-headers?CF-Cache-Status=HIT
  • https://httpbin.org/response-headers?CF-Cache-Status=MISS
  • https://httpbin.org/response-headers?CF-Cache-Status=EXPIRED

More Python Snippets