首页 > 其他 > 详细

生成随机验证码

时间:2015-10-23 18:25:13      阅读:216      评论:0      收藏:0      [点我收藏+]
生成随机验证码实例
import random
a=[]
c =‘‘
for  i in range(6):
    if i == random.randrange(1,4):
        a.append(str(random.randint(1,9)))
    else:
        temp = random.randint(65,90)
        a.append(chr(temp))
print a

b="".join(a)
print b
for i in a:
    c += str(i)
print c

输出为

[‘D‘, ‘J‘, ‘A‘, ‘5‘, ‘B‘, ‘P‘]
DJA5BP
DJA5BP

 

———————知识延伸

1.用到了chr(i),中文说明:返回整数i对应的ASCII字符。与ord()作用相反。

2.字符拼接方式对比。下面是对比消耗时间的例子:

 

import cStringIO
import StringIO
import time
values = []

items = [1,2,31,2,31,2,31,2,3] * 10000000
#print items
st = float(time.time())


for i in items:
    values.append(i)
news = ‘‘.join(values)
print len(news)
print list append cost time:%s%(float(time.time()) - st)

strio = cStringIO.StringIO()
st = float(time.time())
for j in items:
    strio.write(j)
print len(strio.getvalue())
print cStringIO cost time:%s%(float(time.time()) - st)


strio = StringIO.StringIO()
st = float(time.time())
for j in items:
    strio.write(j)
print len(strio.getvalue())
print StringIO cost time:%s%(float(time.time()) - st)

 

生成随机验证码

原文:http://www.cnblogs.com/tudo/p/4904973.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!