"""
写一个用户登录验证程序,文件如下
6666.json:
{"expire_date": "2021-01-01", "debit_id": 6666, "status": 0, "pay_day": 22, "password": "sdf"}
1. 用户id即为json文件名的前缀,密码为password字段,
2. 判断是否过期,与expire_date进行对比。
3. 登陆成功后,打印“登陆成功”,三次登陆失败,status值改为1,并且锁定账号。
4. json文件中用户的密码采用密文保存,使用md5加密算法(计算结果与Linux中md5sum结果一致)
"""
# _*_ coding:utf-8 _*_ # Author: daihaijun import time import json import hashlib def md5sum(password): md5_o = hashlib.md5(bytes(password,encoding="utf-8")) return md5_o.hexdigest() user_file = "6666.json" ufo = open(user_file,'r+') def read_bank_card(): d_card = json.load(ufo) return d_card def write_bank_card(data): str_json = json.dumps(data) ufo.seek(0) ufo.truncate() ufo.write(str_json) ufo.close() data_card = read_bank_card() count = 0 while count < 3: user_input = input("Pls input password:") if md5sum(user_input) == data_card['password']: if data_card['status'] == 1: print("您的账号处于锁定状态,请到柜台进行处理.") break else: if time.strftime('%Y-%m-%d') > data_card['expire_date']: print("Your account has expired.Pls contact counter staff.") else: print("Login sucess!") print(data_card) break else: print("密码错误,请重新输入.") count += 1 continue else: print("密码输入错误已达3次,您的账号已被锁定,请到柜台进行处理.") data_card['status'] = 1 write_bank_card(data_card)