DisCapTy帮助您生成混淆图像,使您的代码易于阅读。
项目描述
DisCapTy
DisCapTy是一个高度类型提示的Python模块,用于生成Captcha图像,无需您费心去制作。任何人都可以使用它!
文档: https://discapty.readthedocs.io/
安装
DisCapTy在PyPi上可用!
pip3 install discapty
要使用DisCapTy,您需要一个Python版本等于或高于3.7
,且低于3.11
。
克隆并测试项目
此项目依赖于Poetry,一个依赖管理工具。您很可能需要此工具才能正确交互项目及其依赖项,请参阅Poetry的文档了解如何安装它。
要克隆仓库: git clone https://github.com/Predeactor/DisCapTy.git
要安装依赖项: poetry install
要运行测试: poetry run python -m unittest
创建Captcha
对于DisCapTy,验证码只是一个可能返回任何对象的代码,例如,它是一个代码(如"12345")和一个图像(通常是一个PIL.Image.Image
对象)。这是因为DisCapTy使用生成器概念,从给定的代码生成验证码,并且可以返回任何内容。
DisCapTy自带3个内置生成器
- TextGenerator:基于文本的验证码
- WheezyGenerator:基于图像的验证码
- ImageGenerator:基于图像的验证码
手动创建验证码
import discapty
def generate_a_captcha(initial_input: str) -> discapty.Captcha:
# This generator returns an obfuscated text.
captcha_for_user = discapty.TextGenerator().generate(initial_input)
# Create a Captcha object, the first argument is the clear code, the second is the obfuscated code. Anything goes.
return discapty.Captcha(initial_input, captcha_for_user)
# Generate your Captcha.
captcha = generate_a_captcha("12345")
# Show the obfuscated code. See https://discapty.readthedocs.io for more information on this object.
show_captcha_to_user(captcha.captcha)
检查用户的输入
import discapty
# Generate your Captcha.
captcha: discapty.Captcha = generate_a_captcha("12345")
# This is your user's input here
user_input: str = '12345'
if captcha.check(user_input) is True:
# The user input is correct
print("Correct!")
else:
# The user input is incorrect
print("Incorrect!")
使用.check
方法的优点是可以指定是否需要从用户输入中删除空格,以及是否需要检查大小写。
手动创建验证码不是推荐的做法,因为DisCapTy自带它的有观点的挑战运行程序,而且效率不高。
创建一个挑战
import discapty
challenge = discapty.Challenge(discapty.TextGenerator(), retries=3)
captcha = challenge.begin()
# We cannot provide typehint here, `captcha` is a `typing.Any` and cannot help you, it'll be your
# job to know what you'll get as a captcha.
send_captcha_to_user(captcha)
user_input: str = get_user_input()
is_correct: bool = challenge.check(user_input)
# If the user's input is correct, the challenge ends, if not, `challenge.attempted_tries` will get
# +1, and if it is greater than the retries that has been set, then an error is raised when using
# `.check`
请参阅文档以了解库的工作方式。
联系方式
您可以通过以下链接加入我的Discord服务器寻求任何帮助:https://discord.gg/WdcFUH7ths
DisCapTy是一个开源项目,采用MIT许可证:
DisCapTy使用Roboto字体作为默认字体。该字体采用Apache-2.0许可证。
项目详情
下载文件
下载您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。