脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python实现用户名和密码登录

Python实现用户名和密码登录

2022-09-14 13:47Jason_Xu_STC Python

这篇文章主要为大家详细介绍了Python实现用户名和密码登录,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Python实现用户名和密码登录的具体代码,供大家参考,具体内容如下

功能

登录及注册,密码错误多次后验证码确认

说明

初次运行,程序将会自动生成一个名为user的文本文档,是包含用户名及密码的字典

输入用户名,如果用户名不存在,程序会自动以输入的用户名进行注册

输入密码,当输错4次时,程序会生成一个4位验证码,并使用vbs方式弹出,如果验证码输错,程序退出,否则重新执行主循环

代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from os import system
from sys import exit
from random import randint
from time import sleep
 
user={'root':'88888888'}
error_time=4
mode=False
chack=[None,None]
user_name=''
user_passwd=[None,None]
 
#读取用户
try:
    f=open('user.txt','r')
    user=eval(f.read())
    f.close()
except:
    f=open('user.txt','w')
    f.write("{'root':'88888888'}")
    f.close
    user={'root':'88888888'}
 
#main
while True:
    user_name=str(input('请输入用户名>'))
    #判断用户是否存在
 
    if user_name not in user:#用户不存在 -> 注册 -> 设置用户名
        print('用户不存在,将执行注册操作。')
        if ' ' in user_name:
            print('\aErr: 用户名中不能有空格')
        elif user_name=='':
            print('\aErr: 用户名不能为空')
        else:
            #设置密码
            while True:
                user_passwd[0]=str(input('请设置密码>'))
                if ' ' in str(user_passwd[0]):
                    print('\aErr: 密码中不能含有空格。')
                elif user_passwd[0]=='':
                    print('\aErr: 密码不能为空。')
                elif len(user_passwd[0])<6:
                    print('\aErr: 密码长度太短,至少6位。')
                else:
                    #再次输入密码
                    user_passwd[1]=str(input('请再次输入密码>'))
                    if user_passwd[0]!=user_passwd[1]:
                        print('\aErr: 两次输入的密码不一致。')
                    else:
                        print('注册成功!\n\n请重新登录:')
                        user[user_name]=user_passwd[0]
                        #写入文件
                        f=open('user.txt','w')
                        f.write(str(user))
                        f.close()
                        break
 
    else:    #用户存在 -> 登录 -> 确认密码是否正确
        #错4次后验证码确认
        while error_time!=0:
            user_passwd[0]=input('请输入密码 4/'+str(error_time)+'>')
            if user_passwd[0]!=user[user_name]:
                print('\aErr: 密码错误')
                error_time=error_time-1
            else:
                mode=True
                break
        else:
            #验证码确认
            print('\n\a\a因错误次数过多,进行验证码确认')
            chack[0]=str(randint(999,10000))    #生成验证码
            #写入到VBS文件,并弹出
            f=open('chack.vbs','w')    
            f.write('msgbox("验证码>'+str(chack[0])+'<")')
            f.close()
            system('start chack.vbs')
            #验证验证码
            chack[1]=str(input('请输入验证码>'))
            if chack[0]!=chack[1]:
                print('\aErr: 验证码错误!')
                #倒计时退出
                for i in range(3,-1,-1):
                    print('\b'*23+'程序将在 '+str(i+1)+' 秒后退出...',end='',flush=True)
                    sleep(1)
                exit(0)
            else:
                error_time=4
 
    if mode==True:
        break
 
input('登录成功...')

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_45447477/article/details/113376242

延伸 · 阅读

精彩推荐