python後端註冊登陸驗證小程序

一共四個文件
python

實現的功能是:註冊帳號,寫到mysql數據庫user(id,name,password,createtime)表中,password字段爲使用md5加密後密碼,並實現密碼驗證登陸。mysql

先上效果圖:sql

一、註冊數據庫

wKioL1mdcZ6DHbizAAA8joFpRFw308.png-wh_50

二、登陸驗證ide

wKiom1mdcdTC9hvIAABNzxhSMXo605.png-wh_50

三、數據庫fetch

wKioL1mdcg-TpaoDAABE0gygpE0863.png-wh_50

說明:數據中24,25是隻加密用戶輸入的密碼字符串,18,19,26,27是加密的name,password,createtime三個字段內容的組合字符,20到23的沒有加密。加密


一、配置文件config.pyspa

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

二、數據庫鏈接文件connect.pyxml

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

三、註冊文件register.pyblog

#!/usr/local/bin/python3
from connect import *
import time
import hashlib

def 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("用戶名已存在!")
				continue
			elif length<6:
				print("用戶名最少6個字符!")
				continue
			elif length>15:
				print("用戶名最多15個字符!")
				continue
			elif 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))
				break
	except:
		conn.rollback()
	else:
		conn.commit()
	conn.close()
	
register()

四、登陸驗證文件login.py

#!/usr/local/bin/python3
from connect import *
import hashlib

def 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=0
		while (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+1
			j=3-i
			if passwd == password_db:
				print("登陸成功!%s,歡迎您。" % name)
				conn.close()
				break
			elif passwd != password_db:
				print("密碼錯誤,請從新輸入!")
				print("您還能夠輸入%s次!" % j)
				continue
			break
	elif count == 0:
		print("您的帳戶不存在!")

login()
相關文章
相關標籤/搜索