import json,time
username=[]
userpasswd=[]
def login_success_file(name):
try:
f=open(r"F:/login_success_syslog.txt",'a+')
json.dump(time.ctime()+":"+name,f)
f.write("
")
except FileNotFoundError as e:
print(e)
def login_fail_file(name):
try:
f=open(r"F:login_faild_syslog.txt",'a+')
json.dump(time.ctime() + ":",name,f)
f.write("
")
except FileNotFoundError as e:
print(e)
def import_to_file():
try:
f1=open(r"F:username",'w',encoding="utf-8")
f2=open(r"F:userpasswd",'w',encoding="utf-8")
json.dump(username,f1)
json.dump(userpasswd,f2)
f1.close()
f2.close()
except FileNotFoundError as e:
print(e)
except Exception as e:
print(e)
def export_from_file():
global username
global userpasswd
try:
f1=open(r"F:username",'r',encoding="utf-8")
f2=open(r"F:userpasswd",'r',encoding="utf-8")
username=json.load(f1)
userpasswd=json.load(f2)
f1.close()
f2.close()
except FileNotFoundError as e:
print(e)
except Exception as e:
print(e)
def creat_user(name,passwd):
export_from_file()
if name in username:
print("usernam has exist!")
name=input("rewrite your username:")
passswd=input("rewrite your userpassswd:")
creat_user(name,passwd)
else:
username.append(name)
userpasswd.append(passwd)
return True
def authentication(name,passwd):
export_from_file()
if name in username:
index=username.index(name)
if passwd==userpasswd[index]:
return True
else:
return False
else:
return False
def user_login_interface():
function=input("登录输入login,注册输入regist:")
if function !="login" and function != "regist":
print("输入错误!")
exit()
name=input("username:")
passwd=input("userpassswd:")
if function=="login":
if authentication(name,passwd)==True:
login_success_file(name)
print("login successfully!")
else:
login_fail_file(name)
print("username or userpasswd error!")
elif function=="regist":
if creat_user(name,passwd)==True:
import_to_file()
print("regist sucessfully!")
user_login_interface()