跟做
"""
登录注册功能
文件打交道用到os
加密用hashlib
存储用字典形式文本json
这三个肯定要调用的
"""
import os
import hashlib
import json
# 先看看存储数据的文件夹在不在,不再就创建
if not os.path.exists(r'user_info'):
os.mkdir(r'user_info')
# 第一个功能先是获取用户名和密码
def get_info():
username = input('please input your username>>>:')
password = input('please input your password>>>:')
if len(username) == 0 or len(password) == 0:
return False
return username, password
def encode(password):
# 获取完就直接加密起来确保用户资料的安全性, 索性把这部分加密封装起来
md5 = hashlib.md5()
md5.update(password.encode('utf8'))
has_pwd = md5.hexdigest()
return has_pwd
# 比对用户名功能
def user_exists(username):
if username in os.listdir(r'user_info'):
return False
return True
# 写入用户信息功能
def write_info(username, has_pwd):
user_dict = {'username': username, 'password': has_pwd}
info_add = os.path.join(r'user_info', username)
with open(info_add, 'w', encoding='utf8') as f:
json.dump(user_dict, f)
def register():
username, password = get_info()
has_pwd = encode(password)
if not user_exists(username):
print('用户名已存在')
return
write_info(username, has_pwd)
print('%s注册成功' % username)
def read_info(username):
info_add = os.path.join(r'user_info', username)
with open(info_add, 'r', encoding='utf8') as f:
info = json.load(f)
return info
def login():
username, password = get_info()
has_pwd = encode(password)
if user_exists(username):
print('用户名不存在,请先注册')
return
info_dict = read_info(username)
if has_pwd == info_dict.get('password'):
print('登录成功')
else:
print('密码错误')
while True:
print("""
注册请输入R
登录请输入L
退出请输入Q
""")
choice = input('请输入您的选择>>>:')
if choice == 'R':
register()
elif choice == 'L':
login()
elif choice == 'Q':
print('您已退出系统')
break
else:
print('您的输入有误请重新输入')
自做
# 先模块导入
import os
import json
import hashlib
# 然后看文件夹有没有创建好
if not os.path.exists(r'user_info'):
os.mkdir(r'user_info')
# 开始添加功能先把输入功能做了
def get():
username = input('请输入用户名').strip()
password = input('请输入密码').strip()
if len(username) == 0 or len(password) == 0:
print('用户名密码不能为空')
return username, password
# 然后是加密功能
def code(password):
md5 = hashlib.md5()
md5.update(password.encode('utf8'))
has_pwd = md5.hexdigest()
return has_pwd
# 检验用户名是否存在
def is_user_exists(username):
if username in os.listdir(r'user_info'): # 用listdir以列表形式打开文件夹做成员运算
return True # 存在返回的是True
return False
# 检验好做写入的功能
def write_in(username, has_pwd):
user_dict = {'username': username, 'password': has_pwd}
info_add = os.path.join(r'user_info', username)
with open(info_add, 'w', encoding='utf8') as f:
json.dump(user_dict, f)
# 下一步把整体功能先做好
def register():
username, password = get()
if is_user_exists(username):
print('用户名已存在')
return
has_pwd = code(password)
write_in(username, has_pwd)
print('%s注册成功' % username)
# 用户名密码比对
def check(username):
info_add = os.path.join(r'user_info', username)
with open(info_add, 'r', encoding='utf8') as f:
data = json.load(f)
return data
def login():
username, password = get()
if not is_user_exists(username):
print('用户名不存在,滚去注册')
return
has_pwd = code(password)
user_dict = check(username) # 获取到用户的一个字典
if has_pwd == user_dict.get('password'):
print('登录成功')
else:
print('密码错误')
while True:
print("""
注册输入1
登录输入2
退出输入3
""")
choice = input('请输入您的指令')
if choice == '1':
register()
elif choice == '2':
login()
elif choice == '3':
break
else:
print('输入有误')
随机验证码检验
import random # 导入模块
def get_rdm_code(figure_num):
random_code = '' # 1.设置一个空的字符串用来接收取到的数据
for i in range(figure_num): # 2.循环取值去取要的数字
num = str(random.randint(0, 9)) # 3.数字随机取0到9
upper = chr(random.randint(65, 90)) # 大写字母随机通过ascii码转换
lower = chr(random.randint(97, 122)) # 同理取小写字母
code = random.choice([num, upper, lower]) # 三者中随机取
random_code += code # 每次取到加给前面
return random_code
print(get_rdm_code(5))
登录注册检验
# 调用模块
import os
import hashlib
import json
if not os.path.exists(r'用户数据'): # 判断文件夹是否存在
os.mkdir(r'用户数据')
def get_info(): # 获取用户名密码
username = input('请输入用户名').strip()
password = input('请输入密码').strip()
if len(username) == 0 or len(password) == 0:
print('用户名或密码不能为空')
return False # 为了和下面返回数据区分开用False告诉函数外有问题
return username, password # 把获取到的值返还出来
def new_pwd(password): # 把密码部分加密并返还
md5 = hashlib.md5()
md5.update(password.encode('utf8')) # 这边注意要记encode用法和文件操作里面不一样
has_pwd = md5.hexdigest()
return has_pwd
def is_user_exists(username): # 查看用户是否存在
if username in os.listdir(r'用户数据'):
return True
return False
def write_info(username, has_pwd):
info_add = os.path.join(r'用户数据', username)
user_dict = {'username': username, 'password': has_pwd}
with open(info_add, 'w', encoding='utf8') as f:
json.dump(user_dict, f)
def register(): # 登录
res = get_info() # 这里把返回的值要赋给res方便之后比对
if not res: # 返回False说明是空用户名或密码 取反走这个流程
return # 结束注册去重新注册
'''这个部分之前都没加,记得要反复看看'''
username, password = res # 没有问题就把两个变量取值掉
if is_user_exists(username):
print('用户名已存在')
return
has_pwd = new_pwd(password)
write_info(username, has_pwd)
print('%s注册成功' % username)
return
def read_info(username):
info_add = os.path.join(r'用户数据', username)
with open(info_add, 'r', encoding='utf8') as f:
info = json.load(f) # 读取出来是个字典赋值给一个变量后反出去
return info
def login(): # 注册
username, password = get_info()
if not is_user_exists(username):
print('用户名不存在')
return # 存在之后还是先把密码搞成密文
has_pwd = new_pwd(password)
info = read_info(username)
real_pwd = info.get('password')
if has_pwd == real_pwd:
print('登录成功')
return
print('密码错误')
while True:
print("""
1注册
2登录
3退出
""")
choice = input('输入指令')
if choice == '1':
register()
elif choice == '2':
login()
elif choice == '3':
break
else:
print('指令不存在')