captcha是pyhton的一个模块,用来生成图片和音频验证码。
安装
pip install captcha
使用
from captcha.audio import AudioCaptcha
from captcha.image import ImageCaptcha
# 加载声音和字体
audio = AudioCaptcha(voicedir='/path/to/voices')
image = ImageCaptcha(fonts=['/path/A.ttf', '/path/B.ttf'])
data = audio.generate('1234')
audio.write('1234', 'out.wav')
data = image.generate('1234')
image.write('1234', 'out.png')
flask验证码
from flask import Flask, Response
from captcha.image import ImageCaptcha
image = ImageCaptcha()
app = Flask(__name__)
@app.route("/captcha")
def captcha_view():
# add your own logic to generate the code
code = "ABCD"
data = image.generate(code)
return Response(data, mimetype="image/png")
参考
https://github.com/lepture/captcha