科探空谷
  • Home
  • zhimind home
  • Categories
  • Tags
  • Archives
  • 留学
    • 学校库
    • 专业库
    • 研究方向与招生
    • 工具
    • GPA计算器
    • 脑洞背单词
    • 脱口而出

Python验证码生成

目录

  • python后端
    • call
    • implement
    • 渲染
  • 前端
    • js
    • html
目录

python后端¶

call¶

code_img, strs = create_validate_code()
session['code_text'] = strs
buf = StringIO.StringIO()
code_img.save(buf, 'JPEG', quality=70)

buf_str = buf.getvalue()
response = app.make_response(buf_str)
response.headers['Content-Type'] = 'image/jpeg'
return response

implement¶

函数create_validate_code代码见validation.py,不太看得懂

渲染¶

前端¶

js¶

document.getElementById("vericode")
        .setAttribute('src','/verifycode?random='+Math.random());

html¶

<div class="form-group">
    <label for="form-code">验证码
    <a href="#"><img onclick="this.setAttribute('src','/verifycode?random='+Math.random())" src="/verifycode" title="点击重新获取" /></a></label>
    <input id="code" name="code" type="text" value="">
</div>

Published

9月 9, 2017

Category

web开发

Tags

  • python 15
  • 网站 2
  • web 11

Stay in Touch

  • Powered by Pelican. Theme: Elegant by Talha Mansoor