方法一,通过choice方式生成验证码

此方法生成每次调用crate_code()会生成三个随机数,然后再三个随机数中选择一个,资源调用相对多些

import random

def v_code(code_length):
res = []
if isinstance(code_length,int):
for i in range(code_length):
ret = create_code()
res.append(ret)
return res
else:
print("请以数字形式输入多少位") def create_code(): #生成随机验证码,通过随机choice的方式
num = chr(random.randint(48, 57)) #随机数字
alfs = chr(random.randint(65, 90)) #随机大写字母
alfb = chr(random.randint(97, 122)) #随机大写字母
s = str(random.choice([num,alfs,alfb]))
return s
#方法二,通过指定一个choice的方式指定
# choice = random.randint(1,3)
# if choice == 1:
# return chr(random.randint(48, 57)) #随机数字
# elif choice ==2:
# return chr(random.randint(65, 90)) #随机大写字母
# elif choice ==3:
# return chr(random.randint(97, 122)) #随机小写字母 if __name__ == "__main__":
code = v_code(4)
for i in range(4):
code[i] = str(code[i])
code_str = ''.join(code)
print("数组类型的展示为:",code)
print("转换成str类型后:",code_str)

方法二,通过random生成choice

此根据choice方式一次生成一次随机数

import random

def v_code(code_length):
res = []
if isinstance(code_length,int):
for i in range(code_length):
ret = create_code()
res.append(ret)
return res
else:
print("请以数字形式输入多少位") def create_code(): #生成随机验证码,通过随机choice的方式
# num = chr(random.randint(48, 57)) #随机数字
# alfs = chr(random.randint(65, 90)) #随机大写字母
# alfb = chr(random.randint(97, 122)) #随机大写字母
# s = str(random.choice([num,alfs,alfb]))
# return s
#方法二,通过指定一个choice的方式指定
choice = random.randint(1,3)
if choice == 1:
return chr(random.randint(48, 57)) #随机数字
elif choice ==2:
return chr(random.randint(65, 90)) #随机大写字母
elif choice ==3:
return chr(random.randint(97, 122)) #随机小写字母 if __name__ == "__main__":
code = v_code(4)
for i in range(4):
code[i] = str(code[i])
code_str = ''.join(code)
print("数组类型的展示为:",code)
print("转换成str类型后:",code_str)

re随机模块应用-生成验证码(无图片)的更多相关文章

  1. python的random模块(生成验证码)

    python的random模块(生成验证码) random模块常用方法 random.random() #生成0到1之间的随机数,没有参数,float类型 random.randint(1, 3) # ...

  2. python ranndom模块及生成验证码

    python的random模块用于生成随机数,下面介绍一下random模块的常用方法: 取随机小数: 数学计算 random.random() 用于生成一个0-1的随机浮点数 0<=n<1 ...

  3. node.js生成验证码及图片

    示例代码: var svgCaptcha = require('svg-captcha'); var fs = require('fs'); var codeConfig = { size: 5,// ...

  4. 详细说说如何生成验证码—ASP.NET细枝末节(4)

    前言 今天小编详细的说一下,ASP.NET网站开发过程中生成验证码的全部问题. 本文的目标,是让读者了解,生成验证码涉及的全部基础知识问题. 当然这里说的是比较简单的验证码. 真正符合要求的验证码,涉 ...

  5. python模块之PIL模块(生成随机验证码图片)

    PIL简介 什么是PIL PIL:是Python Image Library的缩写,图像处理的模块.主要的类包括Image,ImageFont,ImageDraw,ImageFilter PIL的导入 ...

  6. Django随机生成验证码图片

    PIL简介 什么是PIL PIL:是Python Image Library的缩写,图像处理的模块.主要的类包括Image,ImageFont,ImageDraw,ImageFilter PIL的导入 ...

  7. ASP.NET ashx实现无刷新页面生成验证码

    现在大部分网站登陆时都会要求输入验证码,在网上也看了一些范例,现在总结一下如何实现无刷新页面生成验证码. 效果图: 实现方式: 前台: <div> <span>Identify ...

  8. DelphiXE10.2.3——跨平台生成验证码图片

    $("#img-code").bind( 'click', function () { $(this).attr('src','VerifyCode?t='+Math.random ...

  9. captcha ~ 生成验证码图片

    验证码在我们的世界中可以保护我们的信息安全的一个保障之一 这就是生成验证码的代码     # 导报 from captcha.image import ImageCaptcha #验证码的包 from ...

随机推荐

  1. Uncaught TypeError: (intermediate value)(...) is not a function 上一个方法结束没有加分号; 代码解析报错

    Uncaught TypeError: (intermediate value)(...) is not a function 别忽略了,  第一个方法后面的结束 分号; 不起眼的,引来麻烦, 哎,规 ...

  2. The content of element type "web-app" must match "(icon?,display-name?,description?,distributable?,context-param*,filter*,filter-mapping*,listener*,servlet*,servlet- mapping*,session-config?,mime-map

    修改了一下web.xml,加入了一个<filter>,然后就报这样的错??? The content of element type "web-app" must ma ...

  3. leecode第二十一题(合并两个有序链表)

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  4. CC4 表达方式----输赢

    “我要赢,不管付出什么,我一定要赢!”当我赢得时候,“我赢了!(欢呼)”.当我输的时候“不,我不要输.不开心.(垂头丧气)”.这样的场景你是否熟悉呢?我的一生都在经历输赢.以前我会为了赢一场游戏,花费 ...

  5. 日志log4cxx 封装、实例讲解、配置文件log4cxx.properties

    日志log4cxx 封装.实例讲解.配置文件log4cxx.properties 1. 日志作用 程序运行过程中,需要记录程序中的运行状况,方便排查问题,记录数据.可以根据日志的记录快速定位错误发生的 ...

  6. Codeforces 934D - A Determined Cleanup

    934D - A Determined Cleanup 思路: 找规律,和k进制的求法差不多,答案的奇数位是p%k,偶数位如果p%k!=0,那么答案是k-p%k,否则为0. 代码: #include& ...

  7. Axure 元件焦点的控制

    讲解如何控制光标的位置,主要学习了以下三种场景: 1.点击邮箱或者密码时,光标分别自动定位到email.password的文本框处: 2.还有将密码对应的文本框的内容设置为密码的格式(····) 3. ...

  8. ico 图标 生成 工具 网站

    http://www.faviconico.org/favicon favicon.ico在线制作,在线Favicon.ico制作转换工具

  9. Linux下编译安装MySQL

    一.环境准备yum install -y ncurses-devel libaio-develyum install -y cmake makeuseradd -s /sbin/nologin -M ...

  10. Confluence 6 设置其他页面为你空间的主页

    在任何时候,如果你希望某一个页面称为你空间的主页,你可以非常容易的从 编辑空间细节(Edit Space Details)标签页中进行修改. 希望编辑空间的细节: 进入空间后,然后从边栏的底部选择 空 ...