国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python生成驗證碼實例

2019-11-25 18:16:37
字體:
來源:轉載
供稿:網友

本文實例展示了Python生成驗證碼的方法,具有很好的實用價值。分享給大家供大家參考。具體實現方法如下:

前臺頁面代碼如下:

<div> <img id="authcode_img" alt="驗證碼" src="/registration/makeimage/{{time}}"/>   <!-- time 任意隨機數(時間戳),防止頁面緩存 導致驗證碼不能更新--> <a href="javascript:refreshCode();" rel="external nofollow" style="color:blue;">看不清換一張</a></div><script> function refreshCode() {   $('authcode_img').src = "/registration/makeimage/" + Math.random(); }</script>

后臺程序如下:

import StringIOimport Image, ImageDraw, ImageFont, random  #相應的模塊需要安裝from xxx.settings import authcode_font #請確保改字體存在def make_image(request):  mp = hashlib.md5()  mp.update(str(datetime.datetime.now())+str(random.random()))    mp_src = mp.hexdigest()  rand_str = mp_src[0:6]  font = ImageFont.truetype(authcode_font, 25)  width = 75  height = 30  im = Image.new('RGB',(width,height),'#%s'%mp_src[-7:-1])  draw = ImageDraw.Draw(im)  draw.line((random.randint(0,width),random.randint(0,height),random.randint(0,width),random.randint(0,height)))  draw.line((random.randint(0,width),random.randint(0,height),random.randint(0,width),random.randint(0,height)))  draw.line((random.randint(0,width),random.randint(0,height),random.randint(0,width),random.randint(0,height)))  draw.line((random.randint(0,width),random.randint(0,height),random.randint(0,width),random.randint(0,height)))  draw.line((random.randint(0,width),random.randint(0,height),random.randint(0,width),random.randint(0,height)))  draw.text((5,2), rand_str, font=font)    del draw    buffer = StringIO.StringIO()  im.save(buffer,'jpeg')  httpResponse = HttpResponse(content=buffer.getvalue(),mimetype="image/jpeg")  request.session['auth_code'] = rand_str  return httpResponse

程序效果如下:

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宕昌县| 博客| 林西县| 宁波市| 海安县| 乐清市| 额敏县| 怀宁县| 达孜县| 买车| 阿克陶县| 定陶县| 宁化县| 化德县| 碌曲县| 远安县| 吴江市| 舟曲县| 新余市| 新河县| 肃南| 名山县| 宜丰县| 西乌| 宜春市| 梓潼县| 公主岭市| 金阳县| 百色市| 兴海县| 安西县| 普宁市| 延川县| 万山特区| 彭州市| 兖州市| 博客| 大港区| 通许县| 布拖县| 长沙县|