python 之随机生成6位数验证码

简介:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
#用于生成随机数的模块:random
#函数chr()返回对应的ASCII字符,与ord()作用相反。
 
import  random
 
#在1-100之间生成随机数
num  =  random.randint( 1 , 100 )
 
 
#随机生成一个大写字母,ASCII码值65-90对应的是A-Z
cap  =  chr (random.randint( 65 , 90 ))
 
#随机生成一个小写字母,ASCII码值97-122对应的是a-z
low  =  cap  =  chr (random.randint( 97 , 122 ))
 
 
#下面定义一个验证码生成的函数
def  verification_code():
     num  =  random.randint( 100 , 1000 )
     capa  =  chr (random.randint( 65 , 90 ))
     capb  =  chr (random.randint( 65 , 90 ))
     low  =  chr (random.randint( 97 , 122 ))
     vercode  =  capa  +  str (num)  +  capb  +  low
     return  vercode
     
 
#第二种定义
def  ver():
     ver  =  []
     for  in  range ( 6 ):
         if  = =  random.randint( 1 , 9 ):
             ver.append(random.randint( 1 , 9 ))
         else :
             temp  =  random.randint( 65 , 90 )
             ver.append( chr (temp))
     code  =  ''.join(ver)
     return  code
     
     
print  '验证码:%s'  %  verification_code()
print  '验证码:' , ver()
 
 
#第三种定义(推荐做法)
'''
randrange() 方法返回指定递增基数集合中的一个随机数,基数缺省值为1。
random.randrange ([start,] stop [,step])
参数
     start -- 指定范围内的开始值,包含在范围内。
     stop -- 指定范围内的结束值,不包含在范围内。
     step -- 指定递增基数。
 
random模块中randrange()和randint()不同的是前者可以指定一个基数,默认为1
 
'''
 
checkcode  =  ""
 
for  in  range ( 6 ):
     current  =  random.randrange( 0 , 9 , 2 )
     if  current ! =  i:
         temp  =  chr (random.randint( 65 , 90 ))
     else :
         temp  =  random.randint( 0 , 9 )
     checkcode  + =  str (temp)
 
print  checkcode



本文转自 TtrToby 51CTO博客,原文链接:http://blog.51cto.com/freshair/1870789

相关文章
|
1月前
|
数据采集 机器学习/深度学习 安全
Python爬虫之极验滑动验证码的识别
了解极验滑动验证码、特点、识别思路、初始化、模拟点击、识别缺口、模拟拖动。
59 0
|
7月前
|
数据采集 文字识别 测试技术
Python3,这个库,真的是图片类型验证码的克星,真香。
Python3,这个库,真的是图片类型验证码的克星,真香。
43 0
|
7月前
|
Python
Python限定小数位数
Python限定小数位数
16 0
|
1月前
|
数据采集 Web App开发 文字识别
Python爬虫之点触验证码的识别
点触验证码识别思路,初始化,获取,识别。
55 0
Python爬虫之点触验证码的识别
|
1月前
|
数据采集 文字识别 开发者
Python爬虫之图形验证码的识别
python爬虫逆向图形验证码分析,处理和测试实战。
46 0
|
1月前
|
机器学习/深度学习 人工智能 文字识别
Python常用验证码标注和识别(需求分析和实现思路)
Python常用验证码标注和识别(需求分析和实现思路)
41 0
|
1月前
|
存储 安全 JavaScript
使用Python的Flask框架开发验证码登录功能
使用Python的Flask框架开发验证码登录功能
27 0
|
7月前
|
机器人 UED Python
基于Python+Flask实现一个简易网页验证码登录系统案例
基于Python+Flask实现一个简易网页验证码登录系统案例
102 0
基于Python+Flask实现一个简易网页验证码登录系统案例
|
8月前
|
数据采集 算法 开发者
如何使用Python爬虫处理多种类型的滑动验证码
如何使用Python爬虫处理多种类型的滑动验证码
|
3月前
|
Python
Python保留浮点数小数位数的几种常见方法
Python保留浮点数小数位数的几种常见方法
133 0