#!/usr/bin/env python
# -*- coding: utf-8 -*-
#1. 密码长度应该大于或等于8位
#2. 密码必须包含数字、字母及特殊字符三种组合
nums = '0123456789'
chars1 = 'abcdefghijklmnopqrstuvwxyz'
chars2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
symbols = r'''`!@#$%^&*()_+-=/*{}[]\|'";:/?,.<>'''def check_length(pwd):if len(pwd) < 8:print("密码至少需要8位")return Falseelse:return Truedef check_nums(pwd):for each in pwd:if each in nums:return True print("密码应包含数字")return Falsedef check_chars1(pwd):for each in pwd:if each in chars1:return Trueprint("密码应包含小写字母")return Falsedef check_chars2(pwd):for each in pwd:if each in chars2:return Trueprint("密码应包含大写字母")return Falsedef check_symbols(pwd):for each in pwd:if each in symbols:return Trueprint("密码应包含符号")return Falsedef check_password(passwd):# 判断长度length = check_length(passwd)# 判断是否包含数字nums = check_nums(passwd)# 判断是否包含小写字母chars1 = check_chars1(passwd)# 判断是否包含大写字母chars2 = check_chars2(passwd)# 判断是否包含特殊字符symbols = check_symbols(passwd)if all([length, nums, chars1, chars2, symbols]):return Truedef main():password = raw_input('请输入需要检查的密码:')if check_password(password):print("恭喜你通过了强密码检测")if __name__ == '__main__':main()
用正则表达式实现
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import pyperclip
"""
请先将要检测的密码复制到剪切板中再运行程序
密码必须八位或以上,同时包含数字、字母大小写和符号
"""def check_password(passwd):if r1.search(passwd) == None:print('密码应至少为8位')return Falseelif r2.search(passwd) == None:print('密码应包含数字')return Falseelif r3.search(passwd) == None:print('密码应同时包含字母大小写')return Falseelif r4.search(passwd) == None:print('密码应包含符号')return Falseelse:print('密码检测通过!')return Trueif __name__ == '__main__':passwd = str(pyperclip.paste())r1 = re.compile(r'.{8,}')r2 = re.compile(r'\d+')r3 = re.compile(r'[A-Z][a-z]')r4 = re.compile(r'([^a-z0-9A-Z])+')check_password(passwd)
转载于:https://blog.51cto.com/9473774/2064675