Python integrācija

Python var pilnībā aizsargāt — bez pakotnes un bez atkarībām. Pārbaude ir viens vienīgs HTTPS izsaukums.

Mēs nepublicējam Python SDK. Tas attiecas uz mūsu izplatīšanu, nevis uz jūsu aizsardzību: to, ko dara SDK, zemāk redzamais izsaukums paveic ar standarta bibliotēku.

1. Logrīka iekļaušana

Skripts mēra uzvedību pārlūkā un ievieto katrā veidlapā slēptu lauku. Pievienojiet to vienu reizi lapu galvenē.

HTMLhtml
<!-- Add to <head> with SRI for security -->
(function () {
  var KEY = "YOUR_API_KEY";
  var SITE = location.hostname;
  var V = "2025.09.1";
  var s = document.createElement('script');
  s.src = "https://api.silentshield.io/client.js?k=" + encodeURIComponent(KEY)
    + "&v=" + encodeURIComponent(V)
    + "&site=" + encodeURIComponent(SITE);
  s.async = true;
  s.crossOrigin = "anonymous";
  document.head.appendChild(s);
})();

2. Pārbaude serverī

Nosūtīšanas brīdī nolasiet slēpto lauku un pajautājiet mums, pirms pieņemat iesniegumu. Bez šī soļa izlemj tikai pārlūks — un robots bez JavaScript paiet garām.

Pythonpython
# No package required — the check is one HTTPS POST.
# Standard library only: nothing to install, nothing to keep updated.
import json
import os
import urllib.error
import urllib.request

VERIFY_URL = "https://api.silentshield.io/api/v1/captcha/verify-nonce"
API_KEY = os.environ["SILENTSHIELD_KEY"]  # never hard-code it


def is_human(nonce: str) -> bool:
    """Ask SilentShield about one submission.

    The hidden field `behavior_nonce` is injected by client.js;
    read it from the posted form and hand it over unchanged.
    """
    if not nonce:
        return False

    request = urllib.request.Request(
        VERIFY_URL,
        data=json.dumps({"nonce": nonce}).encode(),
        headers={
            "Content-Type": "application/json",
            "X-Api-Key": API_KEY,
            # Tells us which integration is in use, exactly like the SDKs do.
            "X-SS-SDK": "python-inline/1",
        },
        method="POST",
    )

    try:
        with urllib.request.urlopen(request, timeout=5) as response:
            data = json.loads(response.read())
    except (urllib.error.URLError, TimeoutError, ValueError):
        # We are unreachable. Let the visitor through: a real customer turned
        # away costs more than a bot let in. Flip this to False only if you
        # would rather lose submissions than accept one unchecked.
        return True

    return (
        data.get("ok") is True
        and data.get("verdict") == "human"
        and data.get("confidence", 0) >= 0.7
    )


# --- Flask ---------------------------------------------------------------
# @app.post("/contact")
# def contact():
#     if not is_human(request.form.get("behavior_nonce", "")):
#         abort(400, "Please submit the form again.")
#     ...

# --- Django --------------------------------------------------------------
# def contact(request):
#     if not is_human(request.POST.get("behavior_nonce", "")):
#         return HttpResponseBadRequest("Please submit the form again.")
#     ...

# --- FastAPI -------------------------------------------------------------
# @app.post("/contact")
# async def contact(behavior_nonce: str = Form("")):
#     if not is_human(behavior_nonce):
#         raise HTTPException(status_code=400, detail="Please submit the form again.")
#     ...

Flask, Django un FastAPI

Iepriekšējā funkcija nav atkarīga no ietvara. Trīs izsaukuma vietas ir komentāros fragmenta beigās: Flask lasa veidlapas vārdnīcu, Django POST datus, FastAPI veidlapas parametru.

Ja neesam sasniedzami

Fragments apmeklētāju izlaiž cauri. Nepamatoti noraidīts klients maksā vairāk nekā cauri palaists robots. Ja vēlaties pretējo, kļūdas zarā atgrieziet aplami — tad traucējums jums maksās iesniegumus.

MI aģenti

Ziņojiet par aģentu novērojumiem pēc tā paša parauga uz telemetrijas galapunktu: viens POST un tā pati galvene.

Noteikumu vietēja piemērošana no Python nav iespējama — tiek pārbaudīts Ed25519 paraksts noteikumu paketei. Novietojiet enforcer priekšā lietotnei kā sidecar vai izmantojiet Cloudflare Worker.