一款完备的Python用户注册与登录系统(含验证码及哈希加密) | 字数总计: 2.2k | 阅读时长: 9分钟 | 阅读量: |
滴滴滴,沃特陌 与您汇报情况
历经几个周末的时间,我终于将这款用户注册与登录系统 做出来啦~撒花✿✿ヽ(°▽°)ノ✿
一.特性
邮箱验证码验证身份的真实性
验证码错误次数限制
验证码具有时效性 ,超时将会失效(这里设置的是五分钟)
可以用’用户名 ‘或者’邮箱 ‘登录,登录方式不再单一
使用哈希函数 进行加密,用户信息更安全
多用if 等简单语句,小白也能看懂
二.基础要求
Python的基本语法
熟练掌握if ,else ,while ,for 等语句
会使用def 函数
会调用模块 (import module)
了解哈希函数
了解列表 ,字典 的格式与用法
会使用open 等语句读写txt文件以存储用户数据
用到的模块有:json ,hashlib ,random ,string ,os ,time 等
三.代码部分 本系统由四部分代码组成:
users.py (主代码,程序在此运行)
login.py (用户登录时调用)
register.py (用户注册时调用)
check_code.py (发送验证码时调用)
第一部分:uers.py 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 """用户选择登录或注册,这是主代码,在这里运行程序""" import registerimport loginactivekey = 0 while activekey == 0 : choice = input ('你想登录还是注册?\n(键入"注册"或"登录"以继续...)\n(键入"取消"以取消操作...)\n' ) if choice == '注册' : register.register() activekey = 1 elif choice == '登录' : login.login_way() activekey = 1 elif choice == '取消' : activekey = 1 else : print ('请不要键入除"注册"或"登录"以外的字符!!!\n' )
第二部分:login.py 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 94 95 96 97 98 99 100 101 102 103 104 """用户登录""" import hashlibimport check_codedef login_by_username (): check_code.checking() filename = 'users.txt' username = [] active = 0 import os file_exist = os.path.exists(filename) if not file_exist: with open (filename, 'w' ): print ('暂无用户数据,请先注册!!!' ) quit() with open (filename, 'r' ) as file: names = file.read().splitlines() names = [name for name in names] while active == 0 : usernameinput = input ('请输入你的用户名:' ) userpasswordinput = input ('请输入你的密码:' ) if (len (usernameinput.strip()) == 0 ) or (len (userpasswordinput.strip()) == 0 ): print ('用户名或密码不为空,请重试!!!\n' ) continue for name_one in names: name_one = eval (name_one) username.append(name_one['username' ]) if usernameinput in username: for name_two in names: name_two = eval (name_two) if usernameinput == name_two['username' ]: pws = name_two['salt' ] + userpasswordinput after_hash_password = hashlib.sha256(pws.encode("utf-8" )).hexdigest() if after_hash_password == name_two['password_hash' ]: print ('登录成功!!!' ) active = 1 break elif after_hash_password != name_two['password_hash' ]: print ('密码错误,请重试!!!\n' ) break else : print ('用户名不存在,请重试!!!\n' ) def login_by_email (): check_code.checking() filename = 'users.txt' email = [] active = 0 import os file_exist = os.path.exists(filename) if not file_exist: with open (filename, 'w' ): print ('暂无用户数据,请先注册!!!' ) quit() with open (filename, 'r' ) as file: names = file.read().splitlines() names = [name for name in names] while active == 0 : emailinput = input ('请输入你的邮箱:' ) userpasswordinput = input ('请输入你的密码:' ) if (len (emailinput.strip()) == 0 ) or (len (userpasswordinput.strip()) == 0 ): print ('邮箱或密码不为空,请重试!!!\n' ) continue for name_one in names: name_one = eval (name_one) email.append(name_one['email' ]) if emailinput in email: for name_two in names: name_two = eval (name_two) if emailinput == name_two['email' ]: pws = name_two['salt' ] + userpasswordinput after_hash_password = hashlib.sha256(pws.encode("utf-8" )).hexdigest() if after_hash_password == name_two['password_hash' ]: print ('登录成功!!!' ) active = 1 break elif after_hash_password != name_two['password_hash' ]: print ('密码错误,请重试!!!\n' ) break else : print ('邮箱不存在,请重试!!!\n' ) def login_way (): activekey_login = 0 while activekey_login == 0 : login_way = input ('用户名还是邮箱登录?\n(键入"用户名"或"邮箱"以继续...)\n(键入"取消"以取消操作...)\n' ) if login_way == '用户名' : login_by_username() activekey_login = 1 elif login_way == '邮箱' : login_by_email() activekey_login = 1 elif login_way == '取消' : activekey_login = 1 else : print ('请不要键入除"用户名"或"邮箱"以外的字符!!!\n' )
第三部分:register.py 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 """用户注册""" import jsonimport hashlibimport randomimport stringimport check_codedef addsalt (): active = 0 salts = '' while active < 10 : salt_one = random.choice(string.digits) salt_two = random.choice(string.ascii_letters) salt = salt_one + salt_two salts += salt active += 1 return salts before_salt = addsalt() def secret (password ): pws = before_salt + password after_password = hashlib.sha256(pws.encode("utf-8" )).hexdigest() return after_password def register (): """用户注册""" email = check_code.checking() print (f'验证邮箱{email} 将成为注册邮箱' ) filename = 'users.txt' users = {} namecheck = [] import os file_exist = os.path.exists(filename) if not file_exist: with open (filename, 'w' ): print ('你是第一个注册的用户!!!' ) with open (filename, 'r+' ) as file: names = file.read().splitlines() for name in names: name = eval (name) namecheck.append(name['username' ]) namecheck = [name.lower() for name in namecheck] while True : username = input ('请输入您要注册的用户名:' ) if username.lower() in namecheck: print ('用户名已存在,请重试!!!' ) continue elif len (username.strip()) == 0 : print ('用户名不为空!!!' ) continue users['username' ] = username users['email' ] = str (email) password = input ('请输入密码:' ) users['password' ] = password users['password_hash' ] = secret(password) users['salt' ] = before_salt users = json.dumps(users, ensure_ascii=False ) users += f'\n' file.write(users) print (f'注册成功!!!\n您的邮箱为:{email} \n您的用户名为:{username} \n您的密码为:{password} ' ) break
第四部分:check_code.py 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 """验证码的发送与验证""" def checking (): import random to_addr_input = input (f'我们需要验证您身份的真实性\n请输入您的邮箱,以接收验证码:\n' ) check_times = 0 while (to_addr_input.find("@" ) == -1 or to_addr_input.find("." ) == -1 ): if (check_times >= 2 ): print ('您的错误次数过多,请稍后重试...' ) quit() print ("错误的邮箱格式" ) to_addr_input = input ("请输入您的邮箱以接收验证码:\n" ) check_times += 1 def check_code (): active = 0 codes = '' while active < 6 : code = str (random.randint(0 , 9 )) codes += code active += 1 return codes check_codes = check_code() def send_code (): import smtplib from email.mime.text import MIMEText from email.header import Header from_addr = '此处填写你的发件邮箱,用作发送验证码,例:wotemo@qq.com' password = '此处填写你的smtp密钥,例:edghkejyddhjsoid (在QQ邮箱网站中获取)' to_addr = to_addr_input smtp_server = 'smtp.qq.com' msg = MIMEText(f'【沃特陌】您正在进行登录/注册操作,您的验证码为:{check_codes} ,请在10分钟内按页面提示完成验证,切勿将验证码泄露给他人。' , 'plain' , 'utf-8' ) msg['From' ] = Header('沃特陌' ) msg['To' ] = Header('用户' ) msg['Subject' ] = Header('沃特陌邮箱验证码' ) server = smtplib.SMTP_SSL(smtp_server) server.connect(smtp_server, 465 ) server.login(from_addr, password) server.sendmail(from_addr, to_addr, msg.as_string()) server.quit() try : send_code() print ('验证码已发送到您的邮箱中,请注意查收...' ) import time time_start = time.time() except : print ('验证码发送失败,请稍后重试,或反馈客服iswotemo@gmail.com' ) quit() check_code_input = input ('请输入您的验证码:' ) time_end = time.time() time_continue = time_end - time_start check_times = 0 while True : if (time_continue > 300 ): print ('验证码超时,请稍后重试...' ) quit() if (check_times >= 2 ): print ('您的错误次数过多,请稍后重试...' ) quit() if (str (check_code_input) == str (check_codes)): print ('验证通过,请继续您的操作...' ) break else : print ('验证码错误,请重试...' ) check_code_input = input ('请输入您的验证码:' ) check_times += 1 return to_addr_input
四.系统使用
将check_code.py 中的第38 ,39 行代码改为你自己的邮箱和密钥
在users.py 中运行程序,按照提示进行操作即可
如果不想显示明文密码,可将register.py中的第67行代码注释掉
五.进程展示 1.用户注册 展示 2.用户登录 展示 3.邮箱验证码 展示
六.代码获取
Github :https://github.com/wtmxxx/users
七.联系方式
我的博客 :https://www.wotemo.com/
邮箱:iswotemo@gmail.com
Github :https://github.com/wtmxxx/
本代码由沃特陌 (Wotemo )自主编写,如有瑕疵可以和我反映哦~ 如果有不懂的地方也可以和我交流哟(^U^)ノ~YO