python登錄加隨機驗證碼校驗程序(裝飾器內置函數的理解)
使用說明:為了防止惡意攻擊,同時不對原先用戶登錄驗證的方法代碼進行改動,在檢驗賬號密碼方法外再套一層校驗方法(驗證碼),過濾非人為的攻擊。
代碼如下:
#!/usr/bin/env python # -*- coding:utf-8 -*- import os import json import random ###取得絕對路徑下的賬號密碼文件 BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__))) filepath= "%s\\username_passwd" % BASE_DIR print("進入用戶登錄頁面") ###外套的校驗碼程序 def deco(func): def _deco(): randomnum = str(random.randrange(1000, 10000)) print("驗證碼:",randomnum) inputrandomnum=input("請輸入驗證碼:") if inputrandomnum == randomnum: func() return func else: print("驗證碼輸入錯誤") exit() return _deco ### [email protected]
其中裝飾器deco函數內置函數_deco,是為了使deco被初次申明賦值時,不會立即執行裏面的func(),而是等下次調用才執行裏面的func()。
python登錄加隨機驗證碼校驗程序(裝飾器內置函數的理解)