登录注册, 1、注册 2、登录 3、可重复 4、有退出
注册只能使用手机号、密码长度必须在6-12位、密码只能包含数字、字母
只有注册的用户才能登录
import re
from cryptography.fernet import Fernet
class UserLogin:
def __init__(self):
self.user_dict = {}
self.key = Fernet.generate_key()
self.cipher_suite = Fernet(self.key)
def register(self):
username = input("请输入用户名:")
if not self.check_mobile(username):
print('用户名格式不正确')
return
password = input("请输入密码:")
if not self.check_password(password):
print("密码格式不正确")
return
if username in self.user_dict:
print("用户名已存在")
else:
self.user_dict[username] = self.encode_password(password)
print("注册成功")
print(self.user_dict)
def login(self):
username = input("请输入用户名:")
password = input("请输入密码:")
if username in self.user_dict:
if password == self.decode_password(self.user_dict[username]):
print("登录成功")
else:
print("密码错误")
else:
print("用户名不存在")
def check_mobile(self, mobile):
if not re.match('^1[3-9]\d{9}$', mobile):
return False
else:
return True
def check_password(self, password):
if not re.match('[A-Za-z]+[A-Za-z0-9]{5,11}$', password):
return False
else:
return True
def encode_password(self, password):
return self.cipher_suite.encrypt(password.encode('utf-8'))
def decode_password(self, encrypted_data):
return self.cipher_suite.decrypt(encrypted_data).decode('utf-8')
def exit(self):
print("退出成功")
exit()
def main(self):
while True:
print("1、注册 2、登录 3、退出")
choice = input("请输入你的选择:")
if choice == "1":
self.register()
elif choice == "2":
self.login()
elif choice == "3":
self.exit()
else:
print("输入错误")
UserLogin().main()