python ranndom模块及生成验证码

python的random模块用于生成随机数,下面介绍一下random模块的常用方法:

取随机小数:  数学计算
random.random()         用于生成一个0-1的随机浮点数   0<=n<1.0
random.uniform(a,b)     生成一个指定范围内的随机浮点数, a<=n<=b
取随机整数: 彩票 抽奖
random.randint(a,b)           取一个指定范围内的整数   a<=n<=b
random.randrange(start,stop,step) 在指定范围内,按基数递增的集合内取一个随机数,如random.randrange(10,100,2),结果相当于从[10,12,14,...98]序列中获取一个随机数。
从一个序列中随机取值: 抽奖
random.choice()    从序列中随机选择一个返回个数为
random.sample()    从序列中随机选择多个返回,返回的个数为函数的第二个参数
乱序:random.shuffle()  打乱一个列表的顺序,在原列表的基础上直接进行修改,节省空间
验证码的生成:

6位数字验证码:
s = ‘‘
for i in range(6):
    num = random.randint(0,9)
    s += str(num)
print(s)

函数版本的:
def code(n=6):
    s = ‘‘
    for i in range(n):
        num = random.randint(0,9)
        s += str(num)
    return s
print(code(4))
print(code())

6位数字+字母验证码:
def code(n = 6):
    s = ‘‘
    for i in range(n):
        # 生成随机的大写字母,小写字母,数字各一个
        num = str(random.randint(0,9))
        alpha_upper = chr(random.randint(65,90))
        alpha_lower = chr(random.randint(97,122))
        res = random.choice([num,alpha_upper,alpha_lower])
        s += res
    return s
print(code(4))
print(code())

进阶:
def code(n = 6,alpha = True):
    s = ‘‘
    for i in range(n):
        num = str(random.randint(0,9))
        if alpha:
            alpha_upper = chr(random.randint(65,90))
            alpha_lower = chr(random.randint(97,122))
            num = random.choice([num,alpha_upper,alpha_lower])
        s += num
    return s
print(code(4,False))
print(code(alpha=False))

原文地址:https://www.cnblogs.com/feifeifeisir/p/9529404.html

时间: 2024-08-28 18:20:44

python ranndom模块及生成验证码的相关文章

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

python的random模块(生成验证码) random模块常用方法 random.random() #生成0到1之间的随机数,没有参数,float类型 random.randint(1, 3) #生成参数1到参数2之间的随机数,输出为int类型,[1,3] random.randrange(1, 3) #生成参数1到参数2之间的随机数,输出为int类型,[1,3),这个方法还有一种用法,就是下面介绍的这种 random.randrange(0,100,2) #从指定范围内,按指定基数递增的集

随机模块应用-生成验证码(无图片)

方法一,通过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("请以数字形式输入

Python PIL模块随机生成中文验证码

PIL是Python Imaging Library的简称,PIL是一个Python处理图片的库,提供了一系列模块和方法,比如:裁切,平移,旋转,改变尺寸等等.已经是Python平台事实上的图像处理标准库了.PIL功能非常强大,但API却非常简单易用. PIL有如下几个模块:Image模块.ImageChops模块.ImageCrackCode模块.ImageDraw模块.ImageEnhance模块.ImageFile模块.ImageFileIO模块.ImageFilter模块.ImageFo

三条代码 搞定 python 生成验证码

C:\Users\DELL>python Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import qrc

python实战系列之生成随机验证码(03)

背景:在一些登陆网站中,输入用户名和密码之后,通常也需要输入验证码,验证码能够用于加密的salt,防止一些恶意攻击,如下通过python生成任意长度的随机验证码,验证码大写字母,小写字母和数字组成,其中小写字母由97至122的ASIIC码组成,大小字母则有65至90组成,通过chr()函数,将ASIIC码转换为字母,如下通过几行代码即可实现. 程序内容: #!/usr/bin/env python #_*_ coding:utf8 _*_ #author:Happy #来自Happy实验室,该程

Django 生成验证码或二维码 pillow模块

一.安装PIL PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了.PIL功能非常强大,API也非常简单易用. ? PIL模块只支持到Python 2.7,许久没更新了,在python 3.* 版本上使用Pillow模块 ? 安装Pillow ? pip install pillow 二.pillow 基本使用 图像缩放 from PIL import Image # 当前路径打开一个jpg图像文件 img = Image.open('test.

用python生成验证码图片

除了配置好的python环境外,还需要配有python中的PIL库,这是python中专门用来处理图片的库.用传统的pip install 方法或者下载源码 python setup.py install 方法安装该库,很可能会报错(视运行环境不同).可以采用以下方法: 1.下载安装包URL:http://www.pythonware.com/products/pil/index.htm,要下载支持全平台的. 2.解压缩: tar –zxv –f Imaging-1.1.7.tar.gz 3.进

Python Show-Me-the-Code 第 0010 题 生成验证码图片

第 0010 题:使用 Python 生成类似于下图中的字母验证码图片 阅读资料 思路:先随机生成验证码,然后用Python的PIL库画出这个激活码的图片,具体点就是创建画布,加验证码的字上去,增加噪点进行干扰,再进行模糊处理,接着保存到名字为验证码的图片中. 0010.生成验证码图片.py #!/usr/bin/env python #coding: utf-8 import Image, ImageDraw, ImageFont, ImageFilter import string, ran

Python图片处理PIL/pillow/生成验证码/出现KeyError: 和The _imagingft C module is not installed

最近在用Python开发自己的博客,需要用到Python生成验证码,当然肯定要用到Python的图形处理库PIL,因为我用的是windows. 所以在安装好pil之后就开始写,就按照题目所说出现了The _imagingft C module is not installed 错误,找了很多建议,最后确定在windows下应该用pillpw.下载地址 点击打开链接 找到 Pillow?2.5.2.win32?py2.7.exe因为我用的是python2.7和win32系统,所以就应该下载这个,大