导读 | 利用Python库random,string生成大小写字母和数字的随机验证码 |
import random import string def generate_code(bit_num): ''' :param bit_num: 生成验证码位数 :return: 返回生成的验证码 ''' all_str = string.printable.split('!')[0] code = ''.join([random.choice(all_str) for i in range(bit_num)]) return code if __name__ == '__main__': code = generate_code(6) print(code)
生成带特殊符号的验证码如下
import random import string def generate_code(bit_num): ''' :param bit_num: 生成验证码位数 :return: 返回生成的验证码 ''' all_str = string.printable code = ''.join([random.choice(all_str) for i in range(bit_num)]) return code if __name__ == '__main__': code = generate_code(6) print(code)
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/121115.html