Python生成驗(yàn)證碼實(shí)例
本文實(shí)例展示了Python生成驗(yàn)證碼的方法,具有很好的實(shí)用價(jià)值。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
前臺(tái)頁面代碼如下:
<div>
<img id="authcode_img" alt="驗(yàn)證碼" src="/registration/makeimage/{{time}}"/>
<!-- time 任意隨機(jī)數(shù)(時(shí)間戳),防止頁面緩存 導(dǎo)致驗(yàn)證碼不能更新-->
<a href="javascript:refreshCode();" rel="external nofollow" style="color:blue;">看不清換一張</a>
</div>
<script>
function refreshCode() {
$('authcode_img').src = "/registration/makeimage/" + Math.random();
}
</script>
后臺(tái)程序如下:
import StringIO
import Image, ImageDraw, ImageFont, random #相應(yīng)的模塊需要安裝
from xxx.settings import authcode_font #請(qǐng)確保改字體存在
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
程序效果如下:

- Python生成隨機(jī)驗(yàn)證碼的兩種方法
- Python隨機(jī)生成一個(gè)6位的驗(yàn)證碼代碼分享
- python之驗(yàn)證碼生成(gvcode與captcha)
- python生成驗(yàn)證碼圖片代碼分享
- Python 隨機(jī)生成中文驗(yàn)證碼的實(shí)例代碼
- python使用pil生成圖片驗(yàn)證碼的方法
- Python實(shí)現(xiàn)簡單生成驗(yàn)證碼功能【基于random模塊】
- python3 pillow生成簡單驗(yàn)證碼圖片的示例
- python生成隨機(jī)驗(yàn)證碼(中文驗(yàn)證碼)示例
- python生成圖片驗(yàn)證碼的方法
相關(guān)文章
Python3.5 Json與pickle實(shí)現(xiàn)數(shù)據(jù)序列化與反序列化操作示例
這篇文章主要介紹了Python3.5 Json與pickle實(shí)現(xiàn)數(shù)據(jù)序列化與反序列化操作,結(jié)合實(shí)例形式分析了Python3.5使用Json與pickle模塊實(shí)現(xiàn)json格式數(shù)據(jù)的序列化及反序列化操作相關(guān)步驟與注意事項(xiàng),需要的朋友可以參考下2019-04-04
Python隨機(jī)數(shù)種子(random seed)的設(shè)置小結(jié)
隨機(jī)數(shù)種子是控制偽隨機(jī)數(shù)生成器的初始值,通過設(shè)置相同的種子,可以確保隨機(jī)數(shù)序列的一致性,本文主要介紹了Python隨機(jī)數(shù)種子(random seed)的設(shè)置,感興趣的可以了解一下2025-03-03
Flask URL傳參與視圖映射的實(shí)現(xiàn)方法
這篇文章主要介紹了Flask URL傳參與視圖映射的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-03-03
python如何求100以內(nèi)的素?cái)?shù)
在本篇文章里小編給大家分享的是關(guān)于python如何求100以內(nèi)的素?cái)?shù)的方法實(shí)例,需要的朋友們可以學(xué)習(xí)下。2020-05-05
python+pytest自動(dòng)化測試函數(shù)測試類測試方法的封裝
這篇文章主要介紹了python+pytest自動(dòng)化測試函數(shù)測試類測試方法的封裝,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
深入探究Python中的多進(jìn)程模塊用法實(shí)例
多進(jìn)程是計(jì)算機(jī)編程中的一個(gè)概念,也可以說是一種可用于實(shí)現(xiàn)并行性和利用多個(gè) CPU 內(nèi)核或處理器并發(fā)執(zhí)行任務(wù)的技術(shù),在本文中,我們將學(xué)習(xí)有關(guān) python 中多進(jìn)程處理的所有知識(shí)、理論和實(shí)際使用代碼2024-01-01

