Skip to main content

Unofficial Cloudflare Turnstile library for Python with async and sync support.

Project description

Cloudstile

An unofficial Cloudflare Turnstile library with both asynchronous and synchronous support out of the box.

codecov Tests Pylint Mypy

📥 Installation

Cloudstile is available for download via PyPI. To install it, simply do:

pip install cloudstile

🎭 Example

Here are some basic examples of how to validate a user's turnstile token.

[!WARNING] These examples expect the user's IP to be transparent. If you're using something like Cloudflare's proxy service, then you'll need to access the corresponding header for your use case.

🍷 Quart (Asynchronous)

from quart import Quart, request, jsonify
from cloudstile import AsyncTurnstile

app = Quart(__name__)
turnstile = AsyncTurnstile(token="...")

@app.route("/submit", methods=["POST"])
async def submit():

    body = await request.form

    response = await turnstile.validate(
        body.get("cf-turnstile-response", "..."),
        request.remote_addr,
    )

    return jsonify(response.model_dump()) # <- Response is a pydantic object

🏃‍♀️ FastAPI (Asynchronous)

from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from cloudstile import AsyncTurnstile

app = FastAPI()
turnstile = AsyncTurnstile(token="...")

@app.post("/submit")
async def submit(request: Request):

    body = await request.form()

    response = await turnstile.validate(
        body.get("cf-turnstile-response", "..."),
        request.client.host,
    )

    return JSONResponse(response.model_dump()) # <- Response is a pydantic object

🦥 Flask (Synchronous)

from flask import Flask, request, jsonify
from cloudstile import SyncTurnstile

app = Flask(__name__)
turnstile = SyncTurnstile(token="...")

@app.route("/submit", methods=["POST"])
def submit():

    body = request.form

    response = turnstile.validate(
        body.get("cf-turnstile-response", "..."),
        request.remote_addr,
    )

    return jsonify(response.model_dump()) # <- Response is a pydantic object

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cloudstile-2.0.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cloudstile-2.0.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file cloudstile-2.0.1.tar.gz.

File metadata

  • Download URL: cloudstile-2.0.1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.1 Linux/6.8.0-1030-azure

File hashes

Hashes for cloudstile-2.0.1.tar.gz
Algorithm Hash digest
SHA256 7ebe6d1caf7e3a840301f8fe06d3f1a888cf05d96c043522f008b02771397805
MD5 def13726a975046f8437e5dc6670dc93
BLAKE2b-256 821bc7c93a5923b827e4ce9b2df0066afeae56513b8e3391587c7b7398fd3659

See more details on using hashes here.

File details

Details for the file cloudstile-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: cloudstile-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.1 Linux/6.8.0-1030-azure

File hashes

Hashes for cloudstile-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 919a270da2f92a5128347f294c185b0e9a8b93e85f0d1b5b1dc3e7047a01bdda
MD5 c29cd096ce3f0fa576384c794b6d32d7
BLAKE2b-256 1f57a38c2b3604088bff2b824c049e7948b199e2e97b6d3ed461cd8baaf8cad9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page