python入门随机生成验证码并验证是否正确 - Go语言中文社区

python入门随机生成验证码并验证是否正确


  python是我这学期学的一门语言,这是我的第二个大作业就是生成验证码图片并验证。python要注意缩进哟,一旦缩进不对就没法执行程序。

总的思想分为四步

step1:

        生成一个数组(包含26大小写字母和十个数字),我是通过asc码实现的用chr转化为为字符,若直接用数字则会出现int和char型不统一。用的是a.append(x):把x存入a数组中去。随机选择四个元素用的是random.sample(4),要导入一个random的moudle。

step2:

       把随机生成的四个数字生成一张图片。这一步需要导入PIL包,我用的是python2.7需要安装这个包,先去官网下载(https://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy)该包然后可以通过easy_install来安装这是默认安装命令,我用的是pip(pip是一个安装和管理 Python 包的工具),这个也是比较常用的,当时我电脑上也没有配置好pip,需要添加路径在c:/python27:/system把这个添加到环境变量中,这个我也不细说了。除此之外,pip和easy_install也可以用来写在更新包。我把生成验证码图片分为三步,第一步先画一张图然后再把四个元素添加然后再添加线(详细过程看下面代码。)

step3:

        用户输入字符串,这个用来在后台跟生成的四个数作比较,用的是raw_input(),不用input()这个只是用来输入单个字符

step4:

       来判断用户是否输入正确。

       我原来的想法是直接判断输入字符串和随机生成的字符串是否相等,但这样就得分辨大小写过于麻烦,我就想了下面这个方法。把输入的字符串和随机生成的字符串拆成单个字符对应的进行相减,结果不等于0且不等于32(A-a=32)就bool=false且输出bool(bool默认=true),然后跳出比较的循环,如果在循环中没有输出false也就是bool依然等于true,则再进行一次if判断if(bool=true) print('true')。

这是我运行的结果



总代码如下

#!/usr/bin/python
#-*-coding:utf-8 -*-
import random
import string
from PIL import Image,ImageFont,ImageDraw,ImageFilter
#从二十六个大小字母和十个数字随机选出四个数字随机生成一张验证码图片,用户输入(不分大小写)判断用户输入是否正确


#生成随机4个元素
def verCode_text():
Nums=[];
for i in xrange(48,58):
Nums.append(chr(i))#把0~9存入数组中
for i in xrange(97,123):
Nums.append(chr(i))#小写字母
for i in xrange(65,91):
Nums.append(chr(i))#大写字母
a=random.sample(Nums,4)
return a

#生成随机颜色
def verCode_color():
return random.randint(0,65),random.randint(0,65),random.randint(0,65)
#生成背景纹理
def bg_color():
return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))


#生成验证码和图片
def verCode_bg():
#100*30
width=240
height=60
image=Image.new('RGB',(width,height),(255,255,255))
#创建font对象
font=ImageFont.truetype('arial.ttf',36)
#创建draw对象
draw=ImageDraw.Draw(image)
#填充背景元素
for x in range(width):
for y in range(height):
draw.point((x, y), fill=bg_color())
#随机生成二条直线
draw.line((0,0+random.randint(0,height//2),width,0+random.randint(0,height//2)),fill=verCode_color())
draw.line((0,height-random.randint(0,height//2),width,height-random.randint(0,height//2)),fill=verCode_color())
draw.line((0,height-random.randint(0,height//3),width,height-random.randint(0,height//3)),fill=verCode_color())
#输出文字
temp=verCode_text()
codestr=''
for i in range(4):
draw.text((60 * i+ 10, 10),temp[i], font=font, fill=verCode_color())
codestr+=temp[i]
#模糊处理
image = image.filter(ImageFilter.BLUR)
image.save('E:Juniorjunior downpythoncode.png')
#img=image.open('E:Juniorjunior downpythoncode.png')
image.show()
return codestr#生成的字符串
#用户输入字符串
def compare():
c=raw_input("please enter the elements in the picture(The letters are case sensitive):")
return c
    
    
if __name__ == "__main__":
element2=verCode_bg()
list1=list(element2)#字符串分成单个字符存到列表中
list2=list(compare())
c=[]
l1=[]
l2=[]
bo='true'
for i in xrange(0,4):
l1.append(ord(list1[i]))
l2.append(ord(list2[i]))
c.append(abs(l1[i]-l2[i]))
if c[i]!=0 and c[i]!=32:
bo='false'
print('false')
break
if bo!='false':

print('true')


版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/secretqqq/article/details/79640507
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-05-23 20:13:41
  • 阅读 ( 319 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢