Python密码强度 [英] Python password strength

查看:192
本文介绍了Python密码强度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在python上编写密码强度代码,我试图找出我的密码(p)是否包含数字,我已经找到如何查看它是否包含由p.islower().我也把他们两个放在一起了.我的朋友告诉我如何查看密码是否仅包含数字,但我现在需要您的帮助.

I am programming a password strength code on python and i'm trying to find out if my password (p) contains a number, I have found out how to see if it contains upper and lower case letter by p.isupper() or p.islower(). I have also put them two together. My friend has told me how to see if the password only contains number but I need your help now.

running=True
while running:
    p=raw_input("What is your Password? ")
    if len(p) <6:
        print "Your Password is too short"
    if len(p) >12:
        print "Your Password is too long"
    if len(p) == 6 or 7 or 8 or 9 or 10 or 11 or 12:
        print "Password Length OK"
        running=False

print "Loop Broken" #this will be deleted, only for my help now

if p.isupper():
    print "Your Password is weak as it only contains capital letters"

if p.islower():
    print "Your Password is weak as it only contains lower case letters"

if p.isupper and p.islower:
    print "Your Password is of medium strength, try adding some numbers"

try:
    int(p)
    print "Your Password is weak as it only contains numbers"
except (ValueError, TypeError):
    pass

如果密码包含小写或大写字母和数字,我现在需要的是代码.

All I need now is the code for, if the password contains lower or upper case letters and numbers.

推荐答案

对我而言,正则表达式绝对是解决此问题的最简单方法.

To me, regex would definitely be the easiest way of going about this.

给出一个示例密码password,您检查该密码的方式将是:

Given a sample password password, the way you would check it would be:

import re
# Check if contains at least one digit
if re.search(r'\d', password):
    print "Has a digit"
# Check if contains at least one uppercase letter
if re.search(r'[A-Z]', password):
    print "Has uppercase letter"
# Check if contains at least one lowercase letter
if re.search(r'[a-z]', password):
    print "Has lowercase letter"

对于其他作品,您可以继续使用.isupper().islower().

For your other pieces, you can continue to use .isupper() and .islower().

顺便说一下,这部分代码:

By the way, this portion of your code:

if p.isupper and p.islower:
    print "Your Password is of medium strength, try adding some numbers"

将无法正常运行.首先,由于您没有放置括号,因此实际上并没有调用方法-您需要编写if p.isupper() and p.islower():.其次,这实际上并没有达到您想要的目的.您正在尝试检查它是否包含两者小写和大写数字.相反,您要检查这两个字母是否完全是大写,显然不能两者都写,因此if语句将始终返回False.取而代之的是,您将要使用类似以下的内容:

Will not function how you want it to. First, you're not actually calling the methods, since you haven't put the parentheses--you need to write if p.isupper() and p.islower():. Second, this doesn't actually do what you want it to. You're trying to check that it contains both lowercase and uppercase numbers. Instead, you're checking both that it is completely uppercase and completely lowercase, and obviously it can't be both, so that if statement will always return False. Instead, you'll want to use something like:

if re.search(r'[a-z]', password) and re.search(r'[A-Z]', password):

或者,也可以不包含re:

import string
if any(letter in string.ascii_lowercase for letter in password) and \
  any(letter in string.ascii_uppercase for letter in password):

或者:

if any(letter.islower() for letter in password) and \
  any(letter.isupper() for letter in password):

我更喜欢re,因为它更简洁.

I happen to prefer re because it's more concise.

这篇关于Python密码强度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆