码迷,mamicode.com
首页 > 其他好文 > 详细

用户注册登录验证 多版本集合 + hashlib加密

时间:2018-07-08 15:34:28      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:次数   多版本   else   conf   bre   war   ==   密码验证   md5   

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/5/6 0006 12:22
# @Author : Anthony.Waa
# @Site :
# @File : 用户登录(三次错误机会).py
# @Software: PyCharm

# demo1
# 定义用户列表
user_list = [
{‘username‘: ‘anthony‘, ‘password‘: ‘123‘},
{‘username‘: ‘chris‘, ‘password‘: ‘123‘},
{‘username‘: ‘alex‘, ‘password‘: ‘123‘},
{‘username‘: ‘oldboy‘, ‘password‘: ‘123‘},
]

# 定义次数
count = 0

while True:
# 用户名和密码
users = input("请输入你的用户名:")
pasws = input("请输入你的密码:")

# 循环用户列表
for lists in user_list:
if lists[‘username‘] == users and lists[‘password‘] == pasws:
print("登录成功")
exit()
else:
print("登录失败,请重新登陆.")
count += 1
break
if count == 3:
print("你的用户名或密码,错误输入超过3次,退出登陆")
exit()


# ======================================================================================


# demo2
# 定义用户列表
user_list = [
{‘username‘: ‘anthony‘, ‘password‘: ‘123‘},
{‘username‘: ‘chris‘, ‘password‘: ‘123‘},
{‘username‘: ‘alex‘, ‘password‘: ‘123‘},
{‘username‘: ‘oldboy‘, ‘password‘: ‘123‘},
]

# 定义次数
count = 0

# 定义状态
flag = False

while True:
# 用户名和密码
users = input("请输入你的用户名:")
pasws = input("请输入你的密码:")

# 循环用户列表
for lists in user_list:
if lists[‘username‘] == users and lists[‘password‘] == pasws:
flag = True
else:
pass
# 判断登陆状态
if flag:
print("登录成功")
exit()
else:
print("登录失败,请重新登陆.")
count += 1
flag = False
# 判断是否失败次数超过3次
if count == 3:
print("你的用户名或密码,错误输入超过3次,退出登陆")
exit()

====================================================================
# 用户名和密码加密3次登录验证

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/7/8 0008 11:44
# @Author : Anthony.Waa
# @Site :
# @File : auth.py
# @Software: PyCharm

import hashlib
from conf import settings
def login(): # 用户登录
count_end = 3
counts = 1
while True:
username = input(‘请输入用户名:‘).strip()
password = input(‘请输入密码:‘).strip()
verifys = verify(username,password)
if verifys == True:
print(‘登录成功‘)
exit()
else:
count_end -= 1
print(‘账户名或密码输入错误,请重新输入...‘)
print(‘还剩%s次输入机会...‘%count_end)
if counts == 3:
print(‘账户或密码输入错误超过3次‘)
exit()
counts += 1


def verify(username,password): # 用户名和密码验证
md5_obj = hashlib.md5(username.encode(‘utf-8‘))
md5_obj.update(password.encode(‘utf-8‘))
with open(settings.userinfo_path, ‘r‘) as read_verify:
for userinfo_line in read_verify:
if md5_obj.hexdigest() == userinfo_line.strip():
return True


def register(): # 用户注册
while True:
print(‘提示:用户名必须小于8位,密码必须小于12位‘)
username = input(‘请输入用户名:‘).strip()
password = input(‘请输入密码:‘).strip()

if 1 < len(username) <=8 and 1 < len(password) <= 12:
md5_obj = hashlib.md5(username.encode(‘utf-8‘))
md5_obj.update(password.encode(‘utf-8‘))
verifys = verify(username, password) # 验证密码和用户名是否存在

if verifys == True:
print(‘用户名已存在,请重新输入...‘)
else:
with open(settings.userinfo_path,‘a+‘) as write_register:
write_register.write(md5_obj.hexdigest()+‘\n‘)
print(‘注册成功‘)
judge_exit = input(‘是否继续注册(Y/N):‘).strip()
if judge_exit.lower() == ‘y‘:
continue
elif judge_exit.lower() == ‘n‘:
print(‘退出成功‘)
exit()
else:
print(‘输入错误‘)

else:
print(‘用户名或密码位数不符合规定...‘)
continue

login()
register()

用户注册登录验证 多版本集合 + hashlib加密

标签:次数   多版本   else   conf   bre   war   ==   密码验证   md5   

原文地址:https://www.cnblogs.com/ipyanthony/p/8998027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!