激活重新发送OTP按钮,将OTP设置为none,并终止 。否则,计算剩余的分钟和秒 , 将其显示在计时器标签上,并休眠一秒钟 。
def timer_countdown(self): start_time = time.time() while True: current_time = time.time() elapsed_time = current_time - start_time remAIning_time = expiration_time - elapsed_time if self.stop_timer: break if remaining_time <= 0: messagebox.showerror('Error', 'OTP has expired.') self.resend_otp_button.config(state=tk.NORMAL) self.otp = None break minutes = int(remaining_time // 60) seconds = int(remaining_time % 60) timer_label = f'Time Remaining: {minutes:02d}:{seconds:02d}' self.timer_label.config(text=timer_label) time.sleep(1)定义一个方法send_otp() 。如果locked为true,显示相应的消息 。否则提取并验证电话号码,生成一个随机的OTP 。提供之前获取的手机号码,使用客户软件将OTP发送到您的电话号码 。显示消息框 , 启动计时器,禁用按钮 , 并完全清除输入内容 。
def send_otp(self):if self.locked: messagebox.showinfo('Account Locked', 'Your account is locked. Tryagain later.') return mobile_number = self.mobile_number_entry.get() if not mobile_number: messagebox.showerror('Error', 'Please enter your mobile number.') return self.otp = random.randint(1000, 9999) message = client.messages.create( body=f'Your OTP is {self.otp}.', from_='TWILIO_MOBILE_NUMBER', to=mobile_number ) messagebox.showinfo('OTP Sent', f'OTP has been sent to {mobile_number}.') self.start_timer() self.send_otp_button.config(state=tk.DISABLED)self.resend_otp_button.config(state=tk.DISABLED)self.otp_entry.delete(0, tk.END) 【Python构建高效安全的OTP验证系统!】def send_otp(self):if self.locked: messagebox.showinfo('Account Locked', 'Your account is locked. Tryagain later.') return mobile_number = self.mobile_number_entry.get() if not mobile_number: messagebox.showerror('Error', 'Please enter your mobile number.') return self.otp = random.randint(1000, 9999) message = client.messages.create( body=f'Your OTP is {self.otp}.', from_='TWILIO_MOBILE_NUMBER', to=mobile_number ) messagebox.showinfo('OTP Sent', f'OTP has been sent to {mobile_number}.') self.start_timer() self.send_otp_button.config(state=tk.DISABLED)self.resend_otp_button.config(state=tk.DISABLED)self.otp_entry.delete(0, tk.END)定义一个方法resend_otp() 。如果锁住 , 显示相应的消息 。否则获取并验证电话号码,重新生成随机的OTP , 重新发送OTP,显示消息框,启动计时器,并禁用重新发送OTP按钮 。
def resend_otp(self): if self.locked: messagebox.showinfo('Account Locked', 'Your account is locked. Tryagain later.') return mobile_number = self.mobile_number_entry.get() if not mobile_number: messagebox.showerror('Error', 'Please enter your mobile number.') return self.otp = random.randint(1000, 9999) message = client.messages.create( body=f'Your OTP is {self.otp}.', from_='TWILIO_MOBILE_NUMBER', to=mobile_number ) messagebox.showinfo('OTP Sent', f'New OTP has been sent to {mobile_number}.') self.start_timer() self.resend_otp_button.config(state=tk.DISABLED)定义一个方法verify_otp() 。获取OTP,并检查用户是否没有输入任何内容 。如果存储的OTP为None,要求用户先生成OTP 。如果用户输入的OTP与存储的OTP匹配 , 显示OTP验证成功,停止计时器,并退出程序 。否则检查错误的输入尝试 。如果输错次数超过3次,锁住账户 。
def verify_otp(self): user_otp = self.otp_entry.get() if not user_otp: messagebox.showerror('Error', 'Please enter OTP.') return if self.otp is None: messagebox.showerror('Error', 'Please generate OTP first.') return if int(user_otp) == self.otp: messagebox.showinfo('Success', 'OTP verified successfully.') self.stop_timer = Trueexit() else: self.wrong_attempts += 1 if self.wrong_attempts == 3: self.lock_account() else: messagebox.showerror('Error', 'OTP does not match.')定义一个方法lock_account() 。设置锁住状态为true,显示标签为“账户已锁住” 。禁用所有标签、条目和按钮 。停止现有的计时器,启动新的计时器(10分钟) 。
def lock_account(self): self.locked = True self.label1.config(text='Account Locked') self.mobile_number_entry.config(state=tk.DISABLED) self.send_otp_button.config(state=tk.DISABLED) self.timer_label.config(text='') self.resend_otp_button.config(state=tk.DISABLED) self.label2.config(text='') self.otp_entry.config(state=tk.DISABLED) self.verify_otp_button.config(state=tk.DISABLED) self.stop_timer = Truecountdown_time = 10 * 60self.start_countdown(countdown_time)
推荐阅读
- Python日期时间处理与计算:节省时间,准确计算
- 使用LangChain和DeepInfra构建客户支持聊天机器人的操作指南
- 使用Docker构建轻量级Linux容器
- python正则一篇搞掂
- 如何在职场中保持高效率
- 数据复制:构建大规模分布式系统的关键组成部分
- 企业最需要什么样的Python工程师?了解一下!
- 比C语言还快20%!Mojo首个大模型开放下载,性能达Python版250倍
- 物联网边缘技术框架KubeEdge:基于Kubernetes构建的云原生边缘计算框架
- 数字孪生背后的真实构建者:谁在创造数字世界?
