Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Full API reference (all endpoints, error codes, captcha-type details): **https:/
- [Tencent](#tencent)
- [Advanced Usage](#advanced-usage)
- [Check balance](#check-balance)
- [Debug mode](#debug-mode)
- [Custom timeout and polling](#custom-timeout-and-polling)
- [Worker language pool](#worker-language-pool)
- [Async client](#async-client)
Expand Down Expand Up @@ -189,6 +190,7 @@ Constructor.
| `timeout` | `int` | `120` | Polling window after task creation, in seconds. Overridable per call; each HTTP request has a separate 30-second timeout. |
| `polling_interval` | `int` | `10` | Seconds before the first `getTaskResult` poll and between subsequent polls. |
| `language_pool` | `Optional[str]` | `None` | Default worker pool (`"en"` or `"ru"`) applied to every call that doesn't pass its own `language_pool`. |
| `debug` | `bool` | `False` | Emit sanitized request/response, timing, polling, error, and solve summary logs. |

Both clients hold a reusable connection pool (`requests.Session` / `httpx.AsyncClient`)
for their lifetime instead of opening one per request. Close it when you're done --
Expand Down Expand Up @@ -622,7 +624,7 @@ Use this method to identify points that a worker should click in a generic image
| `body` | yes | The captcha image, base64-encoded. |
| `comment` | no (recommended) | Hint for the worker, e.g. `"click on the green apple"`. |
| `imgInstructions` | no | Optional instruction image, base64-encoded. |
| `minClicks` | no | Minimum number of clicks expected (default `1`). |
| `minClicks` | no | Minimum number of clicks expected. When unset, the SDK omits this field and lets the API apply its default. |
| `maxClicks` | no | Maximum number of clicks allowed. |

**Response:** `coordinates` -- a list of `{"x": int, "y": int}` pixel positions to click, in order.
Expand Down Expand Up @@ -684,6 +686,38 @@ balance = client.get_balance()
print(f"Balance: {balance}")
```

### Debug mode

Pass `debug=True` to either client to diagnose an integration. The SDK logs its version and
configuration, each HTTP operation and duration, compact one-line `processing` polls, detailed
sanitized request/response blocks for completed operations and failures, and a `SOLVE SUMMARY`
after every `solve()` call (including failed calls). Debug mode is never enabled from an
environment variable and logging failures do not change request results or SDK exceptions.

```python
client = CaptchaClient("your_api_key", debug=True)
solution = client.solve(task)
```

API credentials, cookies, and proxy credentials are replaced with `[MASKED]`. Proxy type and
port remain visible. Base64 image fields are
replaced with their character count, and other strings longer than 200 characters retain only
their first and last 40 characters plus the original length. The SDK does not log HTTP headers.
URLs and custom task fields can still contain application data, so review logs before sharing
them publicly.

Dedicated synchronous and asynchronous examples demonstrate debug mode:

```bash
python examples/sync/debug_mode.py
python examples/async/debug_mode.py
```

Examples read `CAPTCHA_API_KEY` from the environment or `.env`. They create real paid CAPTCHA
tasks unless their description says otherwise. See
[`examples/sync/debug_mode.py`](examples/sync/debug_mode.py) and
[`examples/async/debug_mode.py`](examples/async/debug_mode.py).

### Custom timeout and polling

```python
Expand Down Expand Up @@ -711,8 +745,8 @@ check the same task later; calling `solve()` again creates a new task.

While polling, `solve()` retries short network failures and HTTP timeouts of
`getTaskResult` on the next poll, so a brief outage does not lose an already
created task. After `poll_retries` (default 3) consecutive failures the error is
raised. API errors are not retried, and `createTask` is never retried because
created task. The SDK tolerates up to `poll_retries` (default 3) consecutive
failures and raises the next one. API errors are not retried, and `createTask` is never retried because
that could create and pay for a second task. To change the limit:

```python
Expand Down
3 changes: 3 additions & 0 deletions captcha_solver_api/_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Internal constants shared by the synchronous and asynchronous clients."""

HTTP_TIMEOUT_SECONDS = 30
239 changes: 239 additions & 0 deletions captcha_solver_api/_debug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
"""Internal, best-effort debug logging helpers."""

from __future__ import annotations

import json
import logging
from datetime import datetime
from typing import Any, Dict, List, Optional

_MASKED_KEYS = {
"authorization",
"clientkey",
"cookie",
"cookies",
"password",
"proxyaddress",
"proxylogin",
"proxypassword",
}
_BASE64_KEYS = {"body", "image", "imginstructions"}
_handler: Optional[logging.Handler] = None


def _sanitize(value: Any, key: str = "") -> Any:
"""Return a sanitized copy suitable for diagnostic output."""
normalized_key = key.lower()
if normalized_key in _MASKED_KEYS:
return "[MASKED]"
if normalized_key in _BASE64_KEYS and isinstance(value, str):
return f"[base64, {len(value)} chars]"
if isinstance(value, dict):
return {item_key: _sanitize(item, str(item_key)) for item_key, item in value.items()}
if isinstance(value, (list, tuple)):
return [_sanitize(item) for item in value]
if isinstance(value, str) and len(value) > 200:
return f"{value[:40]}...[{len(value)} chars]...{value[-40:]}"
return value


def _json(value: Any) -> str:
return json.dumps(_sanitize(value), indent=2, ensure_ascii=False, default=repr)


def _timestamp() -> str:
now = datetime.now().astimezone()
offset = now.strftime("%z")
offset = f"{offset[:3]}:{offset[3:]}"
return now.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] + f" {offset}"


def _error_details(error: BaseException) -> str:
details: List[str] = []
current: Optional[BaseException] = error
seen = set()
while current is not None and len(details) < 4 and id(current) not in seen:
seen.add(id(current))
text = str(current).replace("\n", " ").strip()
description = f"{type(current).__name__}: {text}" if text else type(current).__name__
if not (details and text and str(error) == text):
details.append(description)
current = current.__cause__ or current.__context__
return " <- ".join(details)


class DebugLogger:
"""Emit complete logical events while never affecting SDK behavior."""

def __init__(self, enabled: bool, name: str) -> None:
self.enabled = enabled
self.name = name
self.logger = logging.getLogger(f"captcha_solver_api.{name}")
if enabled:
self._ensure_default_handler()

def _ensure_default_handler(self) -> None:
global _handler
if logging.getLogger().hasHandlers():
return
if _handler is None:
_handler = logging.StreamHandler()
_handler.setFormatter(logging.Formatter("%(message)s"))
if _handler not in self.logger.handlers:
self.logger.addHandler(_handler)

def emit(self, message: str) -> None:
if not self.enabled and not self.logger.isEnabledFor(logging.DEBUG):
return
try:
formatted = f"[{_timestamp()}] [{self.name}] {message}"
if self.enabled:
record = self.logger.makeRecord(
self.logger.name,
logging.DEBUG,
"",
0,
formatted,
(),
None,
)
self.logger.handle(record)
else:
self.logger.debug(formatted)
except Exception:
return

def initialized(
self,
version: str,
base_url: str,
http_timeout: Any,
timeout: Any,
polling: Any,
) -> None:
self.emit(
f"client initialized sdk=python-sdk/{version} baseUrl={base_url} "
f"httpTimeout={http_timeout}s timeout={timeout}s pollingInterval={polling}s"
)

def request_success(
self,
endpoint: str,
url: str,
payload: Dict[str, Any],
data: Dict[str, Any],
status: int,
reason: str,
elapsed_ms: int,
) -> None:
try:
summary = _summary(endpoint, payload, data)
if endpoint == "getTaskResult" and data.get("status") == "processing":
self.emit(f"{summary} ({elapsed_ms}ms)")
return
self.emit(
f"{summary} ({elapsed_ms}ms)\n"
f"REQUEST (POST {url}):\n{_json(payload)}\n"
f"RESPONSE (HTTP {status} {reason}, {elapsed_ms}ms):\n{_json(data)}"
)
except Exception:
return

def request_failure(
self,
endpoint: str,
url: str,
payload: Dict[str, Any],
error: BaseException,
elapsed_ms: int,
status: Optional[int] = None,
reason: str = "",
data: Optional[Any] = None,
invalid_json: bool = False,
body_missing: bool = False,
) -> None:
try:
context = _request_context(endpoint, payload)
label = f"{endpoint} {context}".rstrip()
detail = _error_details(error)
if status is None:
response = (
"RESPONSE: none - no response from the server "
f"({type(error).__name__}, {elapsed_ms}ms)"
)
elif body_missing:
response = (
f"RESPONSE (HTTP {status} {reason}, {elapsed_ms}ms): "
f"body was not received ({type(error).__name__})"
)
elif invalid_json:
response = (
f"RESPONSE (HTTP {status} {reason}, {elapsed_ms}ms): body is not valid JSON"
)
else:
response = f"RESPONSE (HTTP {status} {reason}, {elapsed_ms}ms):\n{_json(data)}"
self.emit(
f"{label} -> failed: {detail} ({elapsed_ms}ms)\n"
f"REQUEST (POST {url}):\n{_json(payload)}\n{response}"
)
except Exception:
return

def solve_summary(
self,
solved: bool,
task_id: Optional[int],
task_type: Optional[str],
elapsed: float,
polls: int,
result: Optional[Dict[str, Any]] = None,
error: Optional[BaseException] = None,
) -> None:
try:
server_time: Optional[str] = None
if result is not None:
created = result.get("createTime")
ended = result.get("endTime")
if isinstance(created, (int, float)) and isinstance(ended, (int, float)):
server_time = f"{ended - created:g}s"
lines = [
"SOLVE SUMMARY:",
f" status: {'solved' if solved else 'not solved'}",
f" taskId: {task_id if task_id is not None else '- (task was not created)'}",
]
if task_type:
lines.append(f" taskType: {task_type}")
lines.append(f" elapsed: {elapsed:.3f}s")
if server_time is not None:
lines.append(f" serverSolveTime: {server_time}")
lines.append(f" polls: {polls}")
if error is not None:
lines.append(f" error: {_error_details(error)}")
self.emit("\n".join(lines))
except Exception:
return


def _request_context(endpoint: str, payload: Dict[str, Any]) -> str:
if endpoint == "createTask":
task = payload.get("task")
return str(task.get("type", "unknown")) if isinstance(task, dict) else "unknown"
if endpoint == "getTaskResult":
return f"taskId={payload.get('taskId', 'unknown')}"
return ""


def _summary(endpoint: str, payload: Dict[str, Any], data: Dict[str, Any]) -> str:
context = _request_context(endpoint, payload)
prefix = f"{endpoint} {context}".rstrip()
if data.get("errorId", 0) != 0:
code = data.get("errorCode", "UNKNOWN_ERROR")
message = data.get("errorDescription", "Unknown API error.")
return f"{prefix} -> failed: ApiError {code}: {message}"
if endpoint == "createTask":
return f"{prefix} -> taskId={data.get('taskId')}"
if endpoint == "getTaskResult":
return f"{prefix} -> {data.get('status', 'unknown')}"
if endpoint == "getBalance":
return f"{prefix} -> balance={data.get('balance')}"
return f"{prefix} -> success"
Loading
Loading