zoukankan      html  css  js  c++  java
  • 模仿网站登录注册

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

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

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

    代码如下

    import hashlib
    import random
    import queryinfo
    dic={}
    path=r'E:PYTHON学习excisesday11usrinformation.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('33[45m 请输入验证码,不区分大小写:33[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
  • 相关阅读:
    MQTT Client软件-MQTTBox
    Eclipse
    Ant + ivy的安装
    常用消息中间件比较
    各种MQTT server功能比較
    消息中间件的对比
    RabbitMQ Performance Testing Tool 性能测试工具
    Eureka 简介
    win10 localhost 解析为::1 的解决办法
    JSP中过滤器的设置
  • 原文地址:https://www.cnblogs.com/yuyang26/p/7059717.html
Copyright © 2011-2022 走看看