Generate Verification Code in Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import random


def generate_verification_code(length=6):
"""随机生成6位的验证码"""
code_list = []
for i in range(10): # 0-9
code_list.append(str(i))
for i in range(ord("A"), ord("Z") + 1):
code_list.append(chr(i))
for i in range(ord("a"), ord("z") + 1):
code_list.append(chr(i))

veri_list = random.sample(code_list, length)
return "".join(veri_list)


if __name__ == "__main__":
print(generate_verification_code())
print(generate_verification_code(8))
1
2
ji0sYH
j8XfT9sh
REF