Category Archives: My Apps

python随机密码生成/password generator – 试试代码高亮

import string
import random

def pwdgen(size = 8):
    if size < 6:
        return 'Length must be at least 6'
    lchars = string.ascii_lowercase
    uchars = string.ascii_uppercase
    digits = string.digits

    schars = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
#    schars = ''
#    for i in range(33,48):
#        schars += chr(i)
#    for i in range(58,65):
#        schars += chr(i)
#    for i in range(91,97):
#        schars += chr(i)
#    for i in range(123,127):
#        schars += chr(i)

    allchars = schars + lchars + uchars + digits

    pwd = []
    pwd.extend([random.choice(schars) for i in range(random.randint(1,2))])
    pwd.extend([random.choice(lchars) for i in range(random.randint(1,2))])
    pwd.extend([random.choice(uchars) for i in range(random.randint(1,2))])
    pwd.extend([random.choice(digits) for i in range(random.randint(1,2))])
    csize = len(pwd)
    pwd.extend([random.choice(allchars) for i in range(size - csize)])

    random.shuffle(pwd)
    return "".join(pwd)

if __name__ == '__main__':
    size = 8
    print pwdgen(size)

#print newpwd