哪个网站教做ppt模板,网站建设试题,广州手机网站建设报价表,域名注册流程及费用本文实例为大家分享了python实现密码强度校验的具体代码#xff0c;供大家参考#xff0c;具体内容如下一 校验规则规则1 密码长度8位以上规则2 密码需包含数字规则3 密码需包含大小写字母规则4 密码需包含特殊字符[, -, *, /供大家参考具体内容如下一 校验规则规则1 密码长度8位以上规则2 密码需包含数字规则3 密码需包含大小写字母规则4 密码需包含特殊字符[, -, *, /, _, , %, ,]规则5 校验5次不通过则强制退出二 文件操作每次输入的密码都会保存到文本文件中以下是python的代码实现作者zhengzhihui版本7.0日期2019/7/13功能判断密码强度2.0功能循环和终止3.0功能将密码保存到文本中4.0功能读取文件遍历文件5.0功能定义PasswordTool类6.0功能定义FileTool类7.0功能密码中增加大小写字母和特殊字符[, -, *, /, _, , %, ,]import time as tmclass FileTool():文件工具类def __init__(self, filepath):self.filepath filepathdef write_to_file(self, content):with open(self.filepath, a) as f:f.write(content)def read_from_file(self):with open(self.filepath, r) as f:content f.readlines()return contentclass PasswordTool():密码工具类def __init__(self, password):self.password passwordself.strength_level 0def check_number_exist(self):判断是否含数字has_number Falsefor c in self.password:if c.isnumeric():has_number Truebreakreturn has_numberdef check_letter_exist(self):判断是否含字母has_upper_letter Falsehas_lower_letter Falsefor c in self.password:if c.isupper():has_upper_letter Trueelif c.islower():has_lower_letter Truehas_both_letter has_upper_letter and has_lower_letterif has_both_letter:breakreturn has_both_letterdef check_specialchar_exist(self):判断是否包含特殊字符has_specialchar Falsespecialchar_list [, -, *, /, _, , %, ,]for c in self.password:if c in specialchar_list:has_specialchar Truebreakreturn has_specialchardef process_password(self):判断是否符合规则# 规则1长度至少8位if len(self.password) 8:self.strength_level 1else:print(密码长度至少8位)# 规则2必须包含数字if self.check_number_exist():self.strength_level 1else:print(密码需要包含数字)# 规则3必须包含大小写字母if self.check_letter_exist():self.strength_level 1else:print(密码需要包含大小写字母)# 规则4需要包含特殊字符if self.check_specialchar_exist():self.strength_level 1else:print(密码需要包含至少一个特殊字符(,-,*,/,_))def main():主函数try_times 5pwd_strength_dict {0: 弱, 1: 较弱, 2: 中, 3: 强, 4: 超强}myfile FileTool(password_7.0.txt)while try_times 0:password input(请输入密码: )mypwdtool PasswordTool(password)mypwdtool.process_password()now_time tm.strftime(%Y-%m-%d %H:%M:%S, tm.localtime())myfile.write_to_file(日期:{} 密码:{} 强度:{}{}.format(now_time, password,mypwdtool.strength_level, pwd_strength_dict[mypwdtool.strength_level]))if mypwdtool.strength_level 4:print(恭喜密码合格)breakelse:print(密码不合格)try_times - 1print()if try_times 0:print(尝试次数过多密码设置失败!)content myfile.read_from_file()print(content)if __name__ __main__:main()以上就是本文的全部内容希望对大家的学习有所帮助也希望大家多多支持云海天教程。