python生成指定长度的验证码

def generate_code(code_len=4):
    """
    生成指定长度的验证码
    """
    # all_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    all_chars = '0123456789'
    last_pos = len(all_chars) - 1
    code = ''
    for i in range(code_len):
        index = random.randint(0,last_pos)
        code += all_chars[index]
    
    return code
print(generate_code(6))

你可能感兴趣的:(python生成指定长度的验证码)