模仿网站登录注册

要求:用户第一次登陆需要注册,如果注册的用户名在文件里已经存在则提示输入新的用户名,将注册的信息更新到文件中,密码使用摘要算法计算得的值进行保存

注册成功即可登录,登录要输入用户名和密码,显示验证码,输入验证码成功后就登陆成功,

下一次运行程序时,直接输入用户名密码即可,无需再次注册。

代码如下

import hashlib
import random
import queryinfo
dic={}
path=r'E:\PYTHON学习\excises\day11\usrinformation.txt'
def file_dic(file):
    with open(file,'r',encoding='utf-8') as f:
        content=f.read()
        dic.update(eval(content))
def get_md5(passwd):
    passwd=passwd.encode(encoding='utf-8')
    md5=hashlib.md5()
    md5.update(passwd)
    return md5.hexdigest()
#=================================
def dic_file(dictionary):
    with open(path,'w',encoding='utf-8') as f:
        f.write(str(dictionary))
#=================================
def display():
    print('input 1 登录')
    print('input 2 注册')
    print('input 3 退出')
#==================================
def createcode():
    res=''
    for i in range(4):
        num=random.randint(0,9)
        word1=chr(random.randint(97,122))
        word2=chr(random.randint(65,90))
        res+=random.choice([str(num),word1,word2])
    return res
def main():
    flag = True
    while flag:
        file_dic(path)      #更新字典
        display()
        num=input('>>')
        if num == '2':
            while True:
                name=input('请输入用户名: ')
                password=input('请输入密码: ')
                if name in dic:
                    print('用户名已经存在,请重新输入')
                else:
                    dic[name] = get_md5(password + name + '1234')
                    dic_file(dic)
                    print('注册成功')
                    break
        elif num == '1':
            while True:
                display_code=createcode()
                print(display_code)
                name = input('请输入用户名:')
                password = input('请输入密码: ')
                code= input('\033[45m 请输入验证码,不区分大小写:\033[0m')
                password = get_md5(password + name + '1234')
                if name in dic and password == dic[name]:
                        if code.strip().lower() == display_code.strip().lower():
                            print('登陆成功')
                        # cmd=input('please input command:')
                        # queryinfo.second_main(cmd)
                            break
                        else:
                            print('验证码输入错误')
                else:
                    print('用户名或者密码错误')
        elif num == '3':
            break
main()
View Code

 

转载于:https://www.cnblogs.com/yuyang26/p/7059717.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值