Captcha solver API errors: a retry strategy in Python
44 minutes ago
Author: admin

Captcha solver API errors: a retry strategy in Python

Captcha solver API errors: a retry strategy in Python

A captcha solver will fail sometimes, and what matters is what your code does next. If every error crashes the job, one empty balance can stop a whole pipeline. This article shows how to handle captcha solver API errors by sorting them into groups, with a small Python example.

Two API styles, one error problem

Solver services use two main protocol styles. The legacy style sends query parameters to /in.php and /res.php, and it answers with plain text such as OK|12345. The newer style posts JSON to /createTask and /getTaskResult.

Error names differ between providers. The reference directory at captchareview.com catalogs 14 error codes used across several solving APIs, so you can compare them in one place. Always confirm the exact meaning in your own provider's documentation.

Sort errors into three groups

Instead of one handler per code, group the codes by what your code should do.

GroupExample codesAction
Fix the setupERROR_KEY_DOES_NOT_EXIST, ERROR_ZERO_BALANCE, ERROR_IP_NOT_ALLOWEDStop and alert a person
Fix the taskERROR_PAGEURL, ERROR_BAD_TOKEN_OR_PAGEURL, ERROR_WRONG_CAPTCHA_IDDo not retry the same input. Log it and check the parameters
Retry laterERROR_NO_SLOT_AVAILABLE, ERROR_CAPTCHA_UNSOLVABLE, ERROR_PROXY_CONNECT_FAILEDWait and retry, with a limit

Based on the directory notes, ERROR_ZERO_BALANCE means the account balance is empty and ERROR_NO_SLOT_AVAILABLE means the queue is full. These two look similar in logs but need opposite handling: the first one needs a person, and the second one needs a short wait.

A fourth case is ERROR_IP_BANNED. The directory describes it as a temporary ban of your client IP after many malformed requests. Slow down and fix the requests before you resume.

A small Python classifier

import random
import time

FATAL = {"ERROR_KEY_DOES_NOT_EXIST", "ERROR_ZERO_BALANCE", "ERROR_IP_NOT_ALLOWED"}
BAD_INPUT = {"ERROR_PAGEURL", "ERROR_BAD_TOKEN_OR_PAGEURL", "ERROR_WRONG_CAPTCHA_ID"}
RETRY = {"ERROR_NO_SLOT_AVAILABLE", "ERROR_CAPTCHA_UNSOLVABLE", "ERROR_PROXY_CONNECT_FAILED"}

class FatalSolverError(Exception):
    pass

def solve_with_retry(solve, attempts=4):
    for n in range(attempts):
        code = solve()  # returns a token, or an error code string
        if not code.startswith("ERROR_"):
            return code
        if code in FATAL:
            raise FatalSolverError(code)
        if code in BAD_INPUT:
            raise ValueError(f"check task parameters: {code}")
        if code in RETRY:
            time.sleep(2 ** n + random.random())
            continue
        raise RuntimeError(f"unknown solver error: {code}")
    raise TimeoutError("solver retries exhausted")

The function name solve is a stand-in for your own wrapper around the provider call. Unknown codes raise an error on purpose: a new code in your logs is a signal to update the lists.

Rules that save money

  • Cap the number of retries. Each solve attempt may cost money.
  • Use exponential backoff with a small random delay.
  • Log the code, the task type and the target domain on every failure.
  • Count solves per target. A sudden rise in ERROR_CAPTCHA_UNSOLVABLE on one target often means that the target changed its challenge.
  • Alert on fatal codes right away. Do not wait for the next daily report.

What to log for each failure

A good log line lets you tell a provider problem from a problem in your own code. Store the raw error code, the task type, the target domain, the attempt number and the time. Do not store the API key. With those fields you can answer simple questions later. Did the errors start after a deploy? Do they come from one target only? Did they appear at the same time as a balance alert? Without this data, every failure looks the same and the team ends up guessing.

Test the handler on open pages

Build the error handling first, before you point the pipeline at protected targets. A public website with open content, such as Indexoid, is an example of a source for practicing request handling and data extraction. Read the site's terms first.

Where the reference stops

The directory is a knowledge base. It is not a live relay and not a latency test harness, so it will not tell you how fast a given service answers today. Use it to understand codes and protocols, and then measure real behavior with your own tasks.

Checklist

  • Map every provider code into one of the groups.
  • Stop on setup errors, and retry only the ones that can recover.
  • Put a hard cap on retries and on spend.
  • Treat unknown codes as a prompt to update your lists.

Discussion

To add a comment, please log in