Registry / auth-security / captcha

captcha

JSON →
library0.7.1pypypi✓ verified 35d ago

The `captcha` library (current version 0.7.1) is a Python utility designed to generate both image and audio CAPTCHAs. It provides a simple API to create various forms of captchas, useful for preventing bots in web applications. The library is actively maintained with regular releases addressing features, bug fixes, and security enhancements.

auth-securityweb-framework
Install & Compatibility
Where this runs
tested against v0.7.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.088s · 37.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.2s · import 0.083s · 39MB
36MB installed
● package 36MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Import paths were restructured in v0.5.0; direct imports from `captcha` no longer work for image/audio modules.

from captcha.image import ImageCaptcha
from captcha.audio import AudioCaptcha

This quickstart demonstrates how to generate a simple image CAPTCHA. It initializes `ImageCaptcha` and then generates an image for a given text, saving it to a file. Note: Font paths may need adjustment based on your operating system. Audio captcha generation is also possible but requires additional dependencies (e.g., `pydub`, `ffmpeg`).

import os from captcha.image import ImageCaptcha # Generate an image captcha image = ImageCaptcha(width=280, height=90, fonts=['/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf']) # Fallback for systems where default font path might not exist # For Windows: C:\Windows\Fonts\arial.ttf # For macOS: /System/Library/Fonts/Arial.ttf # For Linux: Check common paths like /usr/share/fonts/truetype data = image.generate('1234') output_filename = 'captcha_image.png' image.write('1234', output_filename) print(f"Generated captcha image: {output_filename}") # Generate an audio captcha (requires 'pydub' and 'ffmpeg' or 'libav' for advanced audio formats) # from captcha.audio import AudioCaptcha # audio = AudioCaptcha() # audio_filename = 'captcha_audio.wav' # audio.write('1234', audio_filename) # print(f"Generated captcha audio: {audio_filename}")
Debug
Known footguns
breakingThe library underwent a significant code restructuring in version 0.5.0, which changed import paths for `ImageCaptcha` and `AudioCaptcha`.
gotchaVersions prior to 0.7.1 used `secrets.randint` which was later replaced with more secure alternatives. While `secrets.randint` is generally secure, specific edge cases or platform behaviors might have been improved.
gotchaCompatibility with the `Pillow` library is crucial. `captcha` v0.7.0 included updates specifically for Pillow compatibility, indicating potential issues with older `Pillow` versions.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
28 hits · last 30 days
bytedance
9
gptbot
4
ahrefsbot
4
dotbot
2
script
1
bingbot
1
Resources