小玩意

文本登录与验证码系统
本文介绍了一种基于文本文件的用户登录验证系统,并逐步增加了注册功能及验证码验证,最后通过函数化进一步完善了系统的可用性和安全性。
随机验证码

import  random

b = ''
for i in range(5):
    t = random.randrange(0, 9)
    if t==3 or t==0:
        y = random.randrange(0,9)
        u1 = str(y)
    else:
        j = random.randrange(65,91)
        u1 = chr(j)
    b += u1
print (b)

  

文本登录验证
f = open('D:\E\semantic/yess.txt','r',encoding='utf-8')

def func_1(user_2,pwd_2):
    for i in f:
        i1 = i.strip()
        i2 = i1.partition('$')
        if user_2 == i2[0] and pwd_2 == i2[2]:
            return True
    return False

user_1 = input('请输入账号:')
pwd_1 = input('请输入密码')
f = func_1(user_1,pwd_1)
if f:
    print('登录成功')
else:
    print('失败')

  

文本登录验证升级版
f = open('D:\E\semantic/yess.txt','r',encoding='utf-8')


def func_1(user_3,pwd_3):
    for i in f:
        i1 = i.strip()
        i2 = i1.partition('&')
        if user_3 == i2[0] and pwd_3 == i2[2]:
            return True
    return False

print('欢迎登录美国联邦系统,登录请按1,注册请按2')
name = input('')
if name == '2':

    isexit = False
    while not isexit:
        p = False
        user_1 = input('请输入注册账号:')
        for u in f:
            u1 = u.strip()
            u2 = u1.partition('&')
            if user_1 == u2[0]:
                p = True
                break
        if p:
            f.seek(0)
            print('账号已经存在')
            continue
        pwd_1 = input('请输入注册密码')
        print('恭喜你注册成功,欢迎登录美国联邦系统:')
        name_1 = '\n' + user_1 + '&' + pwd_1
        f1 = open('D:\E\semantic/yess.txt','a',encoding='utf-8')
        f1.write(name_1)
        f1.flush()
        f1.seek(0)
        isexit = True
        break



user_1 = input('请输入登录账号:')
pwd_1 = input('请输入登录密码')


r = func_1(user_1,pwd_1)
if r:
    print('登录成功')
else:
    print('失败')

  

函数版:
f = open('D:\E\semantic/yess.txt','r',encoding='utf-8')


def func_1(user_3,pwd_3):
    for i in f:
        i1 = i.strip()
        i2 = i1.partition('&')
        if user_3 == i2[0] and pwd_3 == i2[2]:
            return True
    return False



def Verify(user_name):
    user_pwd = input('请输入注册密码:')
    name = '\n' + user_name + '&' + user_pwd
    f = open('D:\E\semantic/yess.txt','a',encoding='utf-8')
    f.write(name)
    f.flush()
    Sign()

def register():
    while True:
        isexit = False
        isexit_1 = False
        user_2 = input('请输入注册账号:')
        for u in f:
            u1 = u.strip()
            u2 = u1.partition('&')
            if user_2 == u2[0]:
                isexit = True
                f.seek(0)
                break
            else:
                isexit_1 = True
        if isexit:
            print('账号已经存在,请重新注册。')
            continue
        elif isexit_1:
            f.seek(0)
            Verify(user_2)
            break
def Sign():
    inp_1 = input('欢迎登录美国联邦系统,登录请按1,注册请按2.\n')
    if inp_1 == '1':
        user_1 = input('请输入登录账号:')
        pwd_1 = input('请输入登录密码')
        r = func_1(user_1,pwd_1)
        if r:
            print('登录成功')
        else:
            print('失败')
    elif inp_1 == '2':
        register()
Sign()

  

验证码版本:
import  random
f = open('D:\E\semantic/yess.txt','r',encoding='utf-8')


def func_1(user_3,pwd_3):
    for i in f:
        i1 = i.strip()
        i2 = i1.partition('&')
        if user_3 == i2[0] and pwd_3 == i2[2]:
            return True
    return False



def Verify(user_name):
    user_pwd = input('请输入注册密码:')
    name = '\n' + user_name + '&' + user_pwd
    f = open('D:\E\semantic/yess.txt','a',encoding='utf-8')
    f.write(name)
    f.flush()
    Sign()

def register():
    while True:
        isexit = False
        isexit_1 = False
        user_2 = input('请输入注册账号:')
        for u in f:
            u1 = u.strip()
            u2 = u1.partition('&')
            if user_2 == u2[0]:
                isexit = True
                f.seek(0)
                break
            else:
                isexit_1 = True
        if isexit:
            print('账号已经存在,请重新注册。')
            continue
        elif isexit_1:
            f.seek(0)
            Verify(user_2)
            break

def yzm():
    b = ''
    for i in range(5):
        t = random.randrange(0, 9)
        if t==3 or t==0:
            y = random.randrange(0,9)
            u1 = str(y)
        else:
            j = random.randrange(65,91)
            u1 = chr(j)
        b += u1
    return b



def Sign():
    inp_1 = input('欢迎登录美国联邦系统,登录请按1,注册请按2.\n')
    if inp_1 == '1':
        y = yzm()
        user_1 = input('请输入登录账号:')
        pwd_1 = input('请输入登录密码')
        print(y)
        while True:
            yzm_1 = input('请输入验证码')
            yzm_2=yzm_1.upper()
            if y == yzm_2:
                break
            else:
                print('验证码错误')
        r = func_1(user_1,pwd_1)
        if r and y == yzm_2:
            print('登录成功')
        else:
            print('登录失败用户名密码错误:')
    elif inp_1 == '2':
        register()
Sign()

  

函数升级版

#验证账号
def Sign_Sign(user_name):
    f = open('D:\E\semantic/py.txt','r',encoding='utf-8')
    f.seek(0)
    for i in f:
        i1 = i.strip()
        i2 = i1.partition('&')
        if user_name == i2[0]:
            return True
    return False
#验证密码
def pwd_pwd(user_pwd):
    f = open('D:\E\semantic/py.txt','r',encoding='utf-8')
    f.seek(0)
    for i in f:
        i1 = i.strip()
        i2 = i1.partition('&')
        if user_pwd == i2[2]:
            return True
    return False

#添加新密码
def modify_pwd(pwd_modify,pwd_r):
    f = open('D:\E\semantic/py.txt','r',encoding='utf-8')
    f.seek(0)
    list_1 = []
    for i in f:
        i1 = i.strip()
        i2 = i1.partition('&')
        if pwd_modify == i2[0]:
            name = pwd_modify + '&' + pwd_r + '\n'
            list_1.append(name)
            continue
        list_1.append(i)
    print(list_1)
    f1 = open('D:\E\semantic/py.txt','a+',encoding='utf-8')
    f1.seek(0)
    f1.truncate()
    for u in list_1:
        f1.write(u)
    f1.flush
    print('yes')


#主
def Sign():
    inp_1 = input('欢迎登录yellow网,登录请按1,注册请按2.修改密码请按3.\n')
    isexit = False

    #登录
    if inp_1 == '1':
        while not isexit:
            user_1 = input('请输入登录账号:')
            pwd_1 = input('请输入登录密码')
            r = Sign_Sign(user_1)
            print(r)
            b = pwd_pwd(pwd_1)
            print(b)
            if r and b:
                print('登录成功')
                break
            else:
                print('用户名和密码错误,请重新输入:')

    #注册
    elif inp_1 == '2':
        while not isexit:
            user_1 = input('请输入注册账号:')
            r = Sign_Sign(user_1)
            if r:
                print('账号已经存在')
                continue
            break
        pwd_1 = input('请输入注册密码')
        name = '\n' + user_1 + '&' + pwd_1
        f = open('D:\E\semantic/py.txt','a+',encoding='utf-8')
        f.write(name)
        f.flush()
        print('恭喜注册成功,请刷新登录')
        Sign()

    #修改密码
    elif inp_1 == '3':
        while not isexit:
            user_1 = input('请输入登录账号:')
            pwd_1 = input('请输入登录密码')
            r = Sign_Sign(user_1)
            b = pwd_pwd(pwd_1)
            if r and b:
                while not isexit:
                    pwd_s = input('请输入新密码:')
                    pwd_r = input('再次输入新密码')
                    if pwd_r == pwd_s:
                        modify_pwd(user_1,pwd_r)
                        Sign()
                        return
                    else:
                        print('两次密码不一致')
            else:
                print('用户名和密码错误,请重新输入:')
Sign()

  

函数验证码升级版 无bug:
import  random

#验证账号
def Sign_Sign(user_name):
    f = open('D:\E\semantic/py.txt','r',encoding='utf-8')
    f.seek(0)
    for i in f:
        i1 = i.strip()
        i2 = i1.partition('&')
        if user_name == i2[0]:
            return True
    return False
#验证密码
def pwd_pwd(user_pwd):
    f = open('D:\E\semantic/py.txt','r',encoding='utf-8')
    f.seek(0)
    for i in f:
        i1 = i.strip()
        i2 = i1.partition('&')
        if user_pwd == i2[2]:
            return True
    return False

#添加新密码
def modify_pwd(pwd_modify,pwd_r):
    f = open('D:\E\semantic/py.txt','r',encoding='utf-8')
    f.seek(0)
    list_1 = []
    for i in f:
        i1 = i.strip()
        i2 = i1.partition('&')
        if pwd_modify == i2[0]:
            name = pwd_modify + '&' + pwd_r + '\n'
            list_1.append(name)
            continue
        list_1.append(i)
    f1 = open('D:\E\semantic/py.txt','a+',encoding='utf-8')
    f1.seek(0)
    f1.truncate()
    for u in list_1:
        f1.write(u)
    f1.flush

#验证码
def vcation():
    while True:
        vcation_str = ''
        for i in range(5):
            t = random.randrange(0, 9)
            if t==3 or t==0:
                y = random.randrange(0,9)
                u1 = str(y)
            else:
                j = random.randrange(65,91)
                u1 = chr(j)
            vcation_str += u1
        print (vcation_str)
        vcation_1 = input('请输入验证码:')
        vcation_2 = vcation_1.upper()
        if vcation_2 == vcation_str:
            return True
        print('验证码错误请重新输入:')
#主
def Sign():
    inp_1 = input('欢迎登录yellow网,登录请按1,注册请按2.修改密码请按3.\n')
    isexit = False

    #登录
    if inp_1 == '1':
        while not isexit:
            user_1 = input('请输入登录账号:')
            pwd_1 = input('请输入登录密码')
            vcation()
            r = Sign_Sign(user_1)
            b = pwd_pwd(pwd_1)
            if r and b:
                print('登录成功')
                break
            else:
                print('用户名和密码错误,请重新输入:')

    #注册
    elif inp_1 == '2':
        while not isexit:
            user_1 = input('请输入注册账号:')
            vcation()
            r = Sign_Sign(user_1)
            if r:
                print('账号已经存在')
                continue
            break
        pwd_1 = input('请输入注册密码')
        name = '\n' + user_1 + '&' + pwd_1
        f = open('D:\E\semantic/py.txt','a+',encoding='utf-8')
        f.write(name)
        f.flush()
        print('恭喜注册成功,请刷新登录')
        Sign()

    #修改密码
    elif inp_1 == '3':
        while not isexit:
            user_1 = input('请输入登录账号:')
            pwd_1 = input('请输入登录密码')
            vcation()
            r = Sign_Sign(user_1)
            b = pwd_pwd(pwd_1)
            if r and b:
                while not isexit:
                    pwd_s = input('请输入新密码:')
                    pwd_r = input('再次输入新密码')
                    vcation()
                    print('恭喜您已经成功修改密码')
                    if pwd_r == pwd_s:
                        modify_pwd(user_1,pwd_r)
                        Sign()
                        return
                    else:
                        print('两次密码不一致')
            else:
                print('用户名和密码错误,请重新输入:')
Sign()

  

转载于:https://www.cnblogs.com/cloniu/p/6201631.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值