#!/usr/bin/env python
# -*- coding:utf-8 -*-
import hashlib
def md5(arg):
'''
用于把用户的密码加密
'''
md5 = hashlib.md5()
md5.update(bytes(arg, encoding='utf-8'))
return md5.hexdigest()
def register(user,passwd):
#用户注册的时候把密码加密添加到文件
with open("db", 'a') as f:
tmp = "\n" + user + "|"+ md5(passwd)
f.write(tmp)
return True
def login(user,passwd):
#用户登录时候认证
with open('db','r') as f:
for i in f:
c = i.strip()
s = c.split("|")
if s[0]== user and s[1] == md5(passwd):
return True
sum = input("1登录 2注册:")
if sum == "1"or sum == "2":
user = input("请输入账号:")
passwd = input("请输入密码:")
if sum =="1":
if login(user,passwd):
print("登录成功")
else:
print("登录失败")
if sum =="2":
if register(user,passwd):
print("注册成功")
else:
print("输入错误")
转载于:https://blog.51cto.com/caojiaoyue/1964870