python 生成验证码很简单:random和srting的方法

介绍random和string的有关用法,基础的。

  1. sting:------------------获取到0-9十个数字和26个小写大写字母,一共62个。

string.printable:

>>>string.printable
>>>‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\‘()*+,-./:
;<=>[email protected][\\]^_`{|}~ \t\n\r\x0b\x0c‘
>>> text=string.printable
>>> text
‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\‘()*+,-./:
;<=>[email protected][\\]^_`{|}~ \t\n\r\x0b\x0c‘
>>> text[0:62]
‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ‘

2.    获取到62个字符后,要随机从这些字符中选择出4个字符。

random:---------------random.sample(text[0:62],4):

>>> random.sample(text[0:62],4)
[‘k‘, ‘q‘, ‘i‘, ‘S‘]

3.   聚合成字符串sting。

>>> yzCode="".join(random.sample(text[0:62],4))
>>> yzCode
‘0vfN‘
>>> yzCode="".join(random.sample(text[0:62],4))
>>> yzCode
‘fRJq‘
>>> yzCode="".join(random.sample(text[0:62],4))
>>> yzCode
‘ez1q‘
>>> yzCode="".join(random.sample(text[0:62],4))
>>> yzCode
‘qFTG‘

4.   下次会实现把生成的验证码写入到图片上,已达到真正验证码的功能。

时间: 2024-08-03 05:03:26

python 生成验证码很简单:random和srting的方法的相关文章

用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 生成验证码

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 随机生成验证码,应用random和chr函数

四位的随机验证码,chr函数的用法 b=[] for i in range(4): import random c=random.randint(97,122)#仅小写的ascll码范围 b.append(chr(c)) d=''.join(b) print(d) 四位随机验证码,包含大小写和数字 codelist=[] import random for i in range(48,58):#ascll码的数字范围,也可以直接用数字1-10 codelist.append(str(chr(i)

python生成验证码脚本

最近每天都用python写一个小的脚本,练习使用python语法. 验证码的生成: 这里使用了python的图像处理库PIL,安装PIL的过程中出了一个小麻烦,就使用Pillow-win32的一个文件,具体的我也忘了,可以百度下. 直接看代码: # -*- coding:utf-8 -*- from PIL import Image,ImageFont,ImageDraw,ImageFilter import random #返回随机字母 def charRandom(): return chr

python 生成验证码

在工作中经常遇到一些验证码,这些是怎么生成的呢,今天我用Python编写了下 import randomcode = []for i in range(6): if i == random.randint(1,5): code.append(str(random.randint(1,4))) else: tmp = random.randint(65,90) code.append(chr(tmp)) print ''.join(code)

python生成验证码,文字转换为图片

在58或者赶集等一些网站上经常看到手机号是图片格式,或者一些网站的验证码.这些都是动态生成的,今天我们来看一下如何用python把文字生成图片.其实今天主要借助pygame的图像渲染模块,这样比较简单,顺便帮大家复习下pygame这个游戏框架.好啦,直接上代码吧. 环境:python2.7,装有python3的同学也可以测试一下 #coding: UTF-8 #载入必要的模块 import os import pygame from pygame.locals import * #pygame初

Python生成验证码

获取随机字符串 引入PIL包,生成画布. 创建字体,需要使用imagefont.truetype 获取随机背景颜色和字体颜色 将文字写入图像中去 保存图片 代码如下: import random import Image,ImageFilter import ImageFont import ImageDraw #获取随机字符串 def getchar(len=6): #新建元组存储获得的字符串 codelist=[] for i in range(10):#获取数字 codelist.appe

使用 Python 生成验证码(CAPTCHA)

CAPTCHA 图像 Python 数据可视化编程实战代码 点击查看详细内容 from PIL import Image, ImageDraw, ImageFont import random import string class SimpleCaptchaException(Exception): pass class SimpleCaptcha(object): def __init__(self, length=5, size=(200, 100), fontsize=36, rando

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系统,所以就应该下载这个,大