slowdown.captcha – Captcha generator

This module provides a captcha generator implementation for high concurrent services.

Typically, each request generates a captcha. However, this module generates a pool of captcha ahead, it’s not possible to create captcha at each time. The captcha pool refreshes over time.

Example:

>>> c = \
...     Captcha(
...         text_len=4,  # the length of the verification code
...                      # the default is 4
...
...         cache_size=60,  # the size of captcha pool
...                         # the default is 60
...
...         expiration_time=90,  # the expiration time of the captcha
...                              # the default is 90
...
...         image_format= 'jpeg'  # the default is 'jpeg'
...     )
>>> media = c.new()
>>> media.text  # the verification code
7E8a
>>> media.get_img(alt='mycaptcha')
<img src="data:image/jpeg;base64,ZXhhbXBsZQ==' alt='mycaptcha' />
>>> f'<img src="{media.img_src}" />'
<img src="data:image/jpeg;base64,ZXhhbXBsZQ==' />
>>> f'<img src="data:image/jpeg;base64,{media.image_base64}">'
<img src="data:image/jpeg;base64,ZXhhbXBsZQ==' />
>>> with open('mycaptcha.jpeg', 'w') as file_out:  # save to a file
>>>     file_out.write(media.image)
class slowdown.captcha.Captcha(text_len: int = - 1, cache_size: int = - 1, expiration_time: int = - 1, image_format: str = 'jpeg')Captcha

Captcha generator.

new()Media

Acquire a new captcha.

class slowdown.captcha.Media(text, expiration_time, image_generator, image_format)

Captcha data holder.

property image

The raw data of the captcha image.

Return type

bytes

property image_base64

The base64 encoded image.

Return type

str

property img_src

The URL that contains the captcha image for the HTML IMG tag’s src attribute.

Return type

str

get_img(alt: str = None) → str

Returns a HTML IMG tag that contains the captcha image.

property img

get_img(alt:str=None) -> str

Returns a HTML IMG tag that contains the captcha image.