python生成指定长度的验证码

2021-02-20  本文已影响0人  以我丶之姓
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))
上一篇下一篇

猜你喜欢

热点阅读