python後端註冊登入驗證小程式

來源:互聯網
上載者:User

標籤:python   登入驗證   加密   

一共四個檔案

實現的功能是:註冊帳號,寫到mysql資料庫user(id,name,password,createtime)表中,password欄位為使用md5加密後密碼,並實現密碼驗證登入。

先上:

1、註冊

650) this.width=650;" src="https://s3.51cto.com/wyfs02/M01/9F/7D/wKioL1mdcZ6DHbizAAA8joFpRFw308.png-wh_500x0-wm_3-wmp_4-s_129113035.png" title="register.png" alt="wKioL1mdcZ6DHbizAAA8joFpRFw308.png-wh_50" />

2、登入驗證

650) this.width=650;" src="https://s1.51cto.com/wyfs02/M02/00/CD/wKiom1mdcdTC9hvIAABNzxhSMXo605.png-wh_500x0-wm_3-wmp_4-s_2046523173.png" title="login.png" alt="wKiom1mdcdTC9hvIAABNzxhSMXo605.png-wh_50" />

3、資料庫

650) this.width=650;" src="https://s5.51cto.com/wyfs02/M00/9F/7D/wKioL1mdcg-TpaoDAABE0gygpE0863.png-wh_500x0-wm_3-wmp_4-s_1971020331.png" title="shuju.png" alt="wKioL1mdcg-TpaoDAABE0gygpE0863.png-wh_50" />

說明:資料中24,25是只加密使用者輸入的密碼字串,18,19,26,27是加密的name,password,createtime三個欄位內容的組合字元,20到23的沒有加密。


1、設定檔config.py

#mysql info for host,user,passwordhostname="localhost"port="3306"user="login"password="123456"database="login"

2、資料庫連接檔案connect.py

#!/usr/local/bin/python3import pymysqlfrom config import *conn=pymysql.connect(host=hostname,user=user,passwd=password,db=database)cursor=conn.cursor()

3、註冊檔案register.py

#!/usr/local/bin/python3from connect import *import timeimport hashlibdef md5(arg):    md5_pwd = hashlib.md5(bytes(‘abd‘,encoding=‘utf-8‘))    md5_pwd.update(bytes(arg,encoding=‘utf-8‘))    return md5_pwd.hexdigest()def register():try:while True:name=input("輸入你的名字:").strip()cursor.execute("select count(*) from user where name=%s", name)count=cursor.fetchone()[0]length=len(name)if count == 1:print("使用者名稱已存在!")continueelif length<6:print("使用者名稱最少6個字元!")continueelif length>15:print("使用者名稱最多15個字元!")continueelif count == 0 and length>=6 and length=<15:password=input("輸入你的密碼:").strip()time=int(time.time())string=name+password+str(time)passwd=md5(string)cursor.execute("insert into user(name,passwd,createtime) values(%s,%s,%s)",(name,passwd,time))breakexcept:conn.rollback()else:conn.commit()conn.close()register()

4、登入驗證檔案login.py

#!/usr/local/bin/python3from connect import *import hashlibdef md5(arg):    md5_pwd = hashlib.md5(bytes(‘abd‘,encoding=‘utf-8‘))    md5_pwd.update(bytes(arg,encoding=‘utf-8‘))    return md5_pwd.hexdigest()def login():name=input("輸入你的名字:").strip()cursor.execute("select count(*) from user where name=%s",name)count=cursor.fetchone()[0]print(count)if count == 1:i=0while (i<3):cursor.execute("select createtime from user where name=%s",name)time=cursor.fetchone()[0]password=input("輸入你的密碼:").strip()string=name+password+str(time)passwd=md5(string)cursor.execute("select password from user where name=%s",name)password_db=cursor.fetchone()[0]i=i+1j=3-iif passwd == password_db:print("登入成功!%s,歡迎您。" % name)conn.close()breakelif passwd != password_db:print("密碼錯誤,請重新輸入!")print("您還可以輸入%s次!" % j)continuebreakelif count == 0:print("您的賬戶不存在!")login()

本文出自 “從頭再來” 部落格,請務必保留此出處http://4708705.blog.51cto.com/4698705/1958766

python後端註冊登入驗證小程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.