Python登录注册实例

登录注册, 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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值