ITPub博客

首页 > 应用开发 > Python > Python快速生成验证码

Python快速生成验证码

Python 作者:安全剑客 时间:2019-10-07 22:43:32 0 删除 编辑
利用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)

原文地址: https://www.linuxprobe.com/python-string-num.html

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31559985/viewspace-2658988/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2018-11-01

  • 博文量
    1407
  • 访问量
    1394874