Python生成随机验证码代码实例解析
作者:Johnthegreat 时间:2023-08-28 20:48:58
生成6位随机验证码的3种实现方式如下:
1. 简单粗暴型:所有数字和字母都放入字符串;
2. 利用ascii编码的规律,遍历获取字符串和数字的字符串格式;
3. 引用string库。
方法1代码:
import random
_list = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
_result = random.sample(_list, 6)
result = ''.join(_result)
print(result)
方法2代码:
import random
_list = [chr(i) for i in range(48, 58)]
for upper, lower in zip(range(65, 90), range(97, 122)):
_list.append(chr(upper))
_list.append(chr(lower))
_result = random.sample(_list, 6)
result = ''.join(_result)
print(result)
方法3代码:
import string
import random
_list = string.digits + string.ascii_letters
_result = random.sample(_list, 6)
result = ''.join(_result)
print(result)
同时运行,结果如下,3个方法都正确输出了验证码:
moh8n5
SK9scq
PQlaTpProcess finished with exit code 0
如果验证码位数更多,可以替换其中的6为想要的位数。
来源:https://www.cnblogs.com/johnthegreat/p/12790616.html
标签:Python,随机,验证码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SQL Server索引设计基础知识详解使用
2024-01-19 01:11:31
![](https://img.aspxhome.com/file/2023/0/126250_0s.png)
PyQt5实现拖放功能
2023-08-06 11:52:56
![](https://img.aspxhome.com/file/2023/2/103472_0s.png)
PHP 引用的概念
2023-11-14 21:24:28
Go语言omitempty选项的实现
2024-04-25 15:12:40
python之pygame模块实现飞机大战完整代码
2023-08-31 12:10:01
![](https://img.aspxhome.com/file/2023/8/97108_0s.jpg)
浅谈python抛出异常、自定义异常, 传递异常
2022-12-22 00:49:31
Pandas 数据处理,数据清洗详解
2022-05-23 16:04:23
分享10个程序员常用的的代码文本编辑器
2023-03-04 00:53:16
![](https://img.aspxhome.com/file/2023/4/130894_0s.jpg)
怎么写好一份图形界面设计师简历
2009-04-16 13:10:00
oracle下一条SQL语句的优化过程(比较详细)
2010-04-22 16:58:00
ASP简单实现数字和字母验证码
2008-10-23 13:52:00
Python图像的增强处理操作示例【基于ImageEnhance类】
2022-02-12 04:31:46
![](https://img.aspxhome.com/file/2023/3/127883_0s.jpg)
Python Pandas批量读取csv文件到dataframe的方法
2022-12-15 17:05:03
![](https://img.aspxhome.com/file/2023/7/77787_0s.jpg)
Python的Flask框架中配置多个子域名的方法讲解
2021-03-06 19:46:32
golang API请求队列的实现
2024-03-11 16:39:50
JavaScript 全半角转换
2010-02-04 17:14:00
asp如何实现网上考试功能?
2010-05-24 18:32:00
pytorch随机采样操作SubsetRandomSampler()
2021-05-22 18:00:46
谈谈如何手动释放Python的内存
2023-09-22 13:03:57
使用PyTorch实现随机搜索策略
2021-05-16 02:33:02
![](https://img.aspxhome.com/file/2023/4/75864_0s.png)