- import JSON
- import os
- import time as t
- import hashlib
- """
- 导入模块的方式有哪几种, 官方不推荐哪种?
- import 包名
- import module_a #导入
- from module import xx
- from module.xx.xx import xx as rename #导入后重命令
- from module.xx.xx import * #导入一个模块下的所有方法, 不建议使用
- """"""
- 如何让你写的模块可以被系统上任何一个 py 文件导入
- 1, 放在第三方库中 site-package
- from test_import import *
- print(sum1(1,2))
- """"""
- 写一个用户登录验证程序, 文件名 account.json, 内容如下
- {"expire_date": "2021-01-01", "id": 1234, "status": 0, "9": 22, "password": "abc"}
- 1, 根据用户输入的用户名 & 密码, 找到对应的 json 文件, 把数据加载出来进行验证
- 2, 用户名为 json 文件名, 密码为 password.
- 3, 判断是否过期, 与 expire_date 进行对比.
- 4, 登陆成功后, 打印 "登陆成功", 三次登陆失败, status 值改为 1, 并且锁定账号.
- """
- def login():
- count = 1
- while True:
- user_filename = input('请输入您的用户名:')
- password = input('请输入您的密码')
- mulu_path = os.path.dirname(__file__) # 当前所在的目录
- file_path = os.path.join(mulu_path, user_filename)
- if os.path.isfile(file_path): # 判断输入文件所在路径是否正确
- with open(file_path, 'r') as f:
- login_dict = JSON.load(f) # 将文件内容读取出来
- if login_dict['status'] == 0 and count < 4: # 判断是否被锁定 and 登陆次数是否小于 3
- login_password = login_dict['password'] # 登录密码
- expire_date_tuple = t.strptime(login_dict['expire_date'], '%Y-%m-%d') # 将过期时间转为元祖
- expire_date_timestamp = t.mktime(expire_date_tuple) # 将过期时间元祖转为时间戳
- now_timestamp = t.time() # 当前时间戳
- if login_password == password and now_timestamp < expire_date_timestamp: # 密码 和 当前时间小于过期时间都成立(登陆成功)
- print('登陆成功')
- break
- else:
- if count == 3:
- print('您的账户被锁定 1')
- else:
- print('密码错误, 登录失败请重试, 您还剩下 {} 次机会'.format(3 - count))
- else:
- print('您的账户被锁定 2')
- with open(file_path, 'w', encoding='utf-8')as f: # 登录次数超过 3 次时被锁定
- login_dict['status'] = 1
- JSON.dump(login_dict, f)
- break
- else:
- print('用户名错误, 请重新输入')
- count += 1
- # login()
- """
- 写一个用户登录验证程序, 文件名 account.json, 内容如下
- {"expire_date": "2021-01-01", "id": 1234, "status": 0, "9": 22, "password": "abc"}
- 1, 根据用户输入的用户名 & 密码, 找到对应的 json 文件, 把数据加载出来进行验证
- 2, 用户名为 json 文件名, 密码为 password.
- 3, 判断是否过期, 与 expire_date 进行对比.
- 4, 登陆成功后, 打印 "登陆成功", 三次登陆失败, status 值改为 1, 并且锁定账号.
- """
- def login_youhua():
- count = 1
- while count < 4: # 循环 3 次
- uesrname = input('请输入你的用户名:')
- password = input('请输入您的密码:')
- mulu_path = os.path.dirname(__file__) # 当前目录路径
- file_path = os.path.join(mulu_path, uesrname) # 拼接成文件的路径
- try: # 放在 try 中执行
- fr = open(file_path, 'r')
- dict_file = JSON.load(fr)
- fr.close()
- guoqishijian_tuple = t.strptime(dict_file['expire_date'], '%Y-%m-%d') # 时间 元祖
- guoqishijian = t.mktime(guoqishijian_tuple) # 过期时间的时间戳
- now_time = t.time() # 当前时间的 时间戳
- if dict_file['status'] == 0: # 判断是否状态为 0
- if password == dict_file['password']: # 判断是否登陆成功
- if now_time < guoqishijian: # 判断是否过期
- print('登陆成功')
- break
- else:
- print('已过期')
- break
- else:
- if count == 3: # 如果输入了三次将 dict_file['status'] = 1 写入文件
- print('账号已锁定')
- with open(file_path, 'w', encoding='utf-8')as f:
- dict_file['status'] = 1
- JSON.dump(dict_file, f)
- break
- else: # 否则提示密码错误
- print('密码错误')
- else:
- print('已锁定') # 否则提示已过期退出程序
- break
- except Exception as e:
- print('出错了{}'.format(e.args))
- count += 1 # count 放在这里, 如果文件名输入错误三次也退出
- # login_youhua()
- """把第 3 题用户密码进行 hashlib 加密处理. 即: json 文件里的密码保存为 md5 的值, 然后用 md5 的值进行验证账号信息是否正确."""
- def login_youhua_hash():
- count = 1
- m = hashlib.md5()
- while count < 4: # 循环 3 次
- uesrname = input('请输入你的用户名:')
- password_ming = (input('请输入您的密码:'))
- m.update(password_ming.encode('utf-8')) # 将输入的密码传入到 md5 的对象 m 中
- password = m.hexdigest() # 将 16 进制形式的密码赋值给 password
- mulu_path = os.path.dirname(__file__) # 当前目录路径
- file_path = os.path.join(mulu_path, uesrname) # 拼接成文件的路径
- try: # 放在 try 中执行
- fr = open(file_path, 'r')
- dict_file = JSON.load(fr)
- fr.close()
- guoqishijian_tuple = t.strptime(dict_file['expire_date'], '%Y-%m-%d') # 时间 元祖
- guoqishijian = t.mktime(guoqishijian_tuple) # 过期时间的时间戳
- now_time = t.time() # 当前时间的 时间戳
- if dict_file['status'] == 0: # 判断是否状态为 0
- if password == dict_file['password']: # 判断是否登陆成功
- if now_time < guoqishijian: # 判断是否过期
- print('登陆成功')
- break
- else:
- print('已过期')
- break
- else:
- if count == 3: # 如果输入了三次将 dict_file['status'] = 1 写入文件
- print('账号已锁定')
- with open(file_path, 'w', encoding='utf-8')as f:
- dict_file['status'] = 1
- JSON.dump(dict_file, f)
- break
- else: # 否则提示密码错误
- print('密码错误')
- else:
- print('已锁定') # 否则提示已过期退出程序
- break
- except Exception as e:
- print('出错了{}'.format(e.args))
- count += 1 # count 放在这里, 如果文件名输入错误三次也退出
- login_youhua_hash()
来源: http://www.bubuko.com/infodetail-3493970.html