Python-登錄注冊驗證及加密


''' 需求: 1、注冊的時候存賬戶密碼到數據庫里面,密碼存密文,要加鹽 2、登錄的時候賬號、密碼從數據里面取,登錄失敗一次,錯誤次數+1,錯誤次數大於3不讓登錄 思路: 注冊: 1、建表 id,username,password,error_count 2、賬號不存在,並且2次輸入的密碼是一致,就插入到數據庫里面 登錄: 1、賬號密碼 2、select password,error_count from nhy_user where username = '%s' % username 3、取到error_count,判斷是否大於3 4、把用戶輸入的密碼MD5一下,和數據庫里面存的密碼對比 '''

import pymysql,hashlib def op_mysql(sql:str): mysql_info = { 'host': 'XXX.XXX.XXX.XXX', 'port': XXX, 'password': 'XXX', 'user': 'XXX', 'db': 'XXX', 'charset': 'utf8', 'autocommit': True } result = '執行完成' conn = pymysql.connect(**mysql_info) cur = conn.cursor(pymysql.cursors.DictCursor) #建立游標
 cur.execute(sql) if sql.strip().lower().startswith('select'): result = cur.fetchone() # result = cur.fetchall()
 cur.close() conn.close() return result def md5(s,salt='$!@#$12232'): s = (str(s)+salt).encode() m = hashlib.md5(s)#加密
    return m.hexdigest() def register(): # 注冊
    for i in range(3): username = input('username:').strip() passwd = input('password:').strip() cpasswd = input('cpassword:').strip() if username and passwd and cpasswd: if passwd == cpasswd: sql="select * from nhy_user where username='%s';" % username if op_mysql(sql): print('用戶已經存在!') else: md5_password = md5(passwd) sql2 = 'insert into nhy_user (username,password) value ("%s","%s");'%(username,md5_password) op_mysql(sql2) print('注冊成功!') else: print('兩次密碼輸入不一致') else: print('賬號/密碼不能為空') def login(): # 登錄
    username = input('username:').strip() passwd = input('passwd:').strip() if username and passwd: sql = "select passwd,error_count from app_myuser where username = '%s';"%username res = op_mysql(sql) # print('res======',type(res))
        if res: if res.get('error_count')>2: print('用戶已經被鎖定') else: new_passwd = md5(passwd) if res.get('password') == new_passwd: print('登錄成功!') else: print('密碼輸入錯誤!') count = res.get('error_count')+1 sql2 = 'update nhy_user set error_count = %s where username = "%s";'%( count,username ) op_mysql(sql2) else: print('用戶不存在') def clear_error_count(): username = input('username:').strip() sql2 = 'update nhy_user set error_count = 0 where username = "%s";'%username op_mysql(sql2)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM