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)簡(jiǎn)單生成驗(yàn)證碼功能【基于random模塊】
- python3 pillow生成簡(jiǎn)單驗(yàn)證碼圖片的示例
- python生成隨機(jī)驗(yàn)證碼(中文驗(yàn)證碼)示例
- python生成圖片驗(yàn)證碼的方法
相關(guān)文章
python 從遠(yuǎn)程服務(wù)器下載日志文件的程序
本文提供了一種方法,該程序可實(shí)現(xiàn)抓取服務(wù)器的日志文件到本地,根據(jù)該程序可自行擴(kuò)展對(duì)日志文件的自動(dòng)化分析。常用于數(shù)據(jù)挖掘、生產(chǎn)維護(hù)等2013-02-02Flask運(yùn)用Xterm實(shí)現(xiàn)交互終端的示例詳解
Xterm是一個(gè)基于X Window System的終端仿真器(Terminal Emulator),Xterm最初由MIT開發(fā),它允許用戶在X Window環(huán)境下運(yùn)行文本終端程序,本文給大家介紹了Flask運(yùn)用Xterm實(shí)現(xiàn)交互終端的示例詳解,文中有詳細(xì)的代碼講解,需要的朋友可以參考下2023-11-11使用python解析MDX詞典數(shù)據(jù)并保存為Excel文件
MDX(Mobile Dictionary eXchange)是一種常見的詞典文件格式,通常用于在移動(dòng)設(shè)備和電腦之間共享辭典數(shù)據(jù),本文深入探討了從MDX詞典數(shù)據(jù)提取、處理到最終保存為Excel文件的全過程,需要的朋友可以參考下2023-12-12Python將腳本程序轉(zhuǎn)變?yōu)榭蓤?zhí)行程序的實(shí)現(xiàn)
本文主要介紹了Python將腳本程序轉(zhuǎn)變?yōu)榭蓤?zhí)行程序的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Python數(shù)據(jù)結(jié)構(gòu)與算法之完全樹與最小堆實(shí)例
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法之完全樹與最小堆,結(jié)合實(shí)例形式分析了Python完全樹定義及堆排序功能實(shí)現(xiàn)相關(guān)操作技巧,需要的朋友可以參考下2017-12-12Python中列表遍歷使用range和enumerate的區(qū)別講解
這篇文章主要介紹了Python中列表遍歷使用range和enumerate的區(qū)別,在Python編程語言中,遍歷list有range和enumerate方法,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12