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.

You can find more documentation on how to use Cloudstile in our examples or our wiki.

codecov

📥 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.0.tar.gz (4.6 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.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cloudstile-2.0.0.tar.gz
  • Upload date:
  • Size: 4.6 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.0.tar.gz
Algorithm Hash digest
SHA256 f7dc03ddb0dea6e6da253caaed4da01339ea1a483c0002859c42acd33b955bb0
MD5 7b4f86ae017cd736f0ec9dc502ef093c
BLAKE2b-256 b776a5dad489d0d5ecb9b2c518987632b9429cbd73449b67847aca617049bb99

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cloudstile-2.0.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6de46b75203a7cee85e79e10d05d205dd8921e572e3c00523ae811b8d60ae876
MD5 38237f709a960e38c8de7d9d7b437f26
BLAKE2b-256 edeff49af20677022bef9dc0aabb99860fff4a4b4f4a515099b45de8ee4d286e

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