随机验证码生成(python实现)
需求:生成随机不重复验证码。
代码:
#!/usr/bin/env python
# encoding: utf-8
"""
@author: 侠之大者kamil
@file: 200number.py
@time: 2016/4/13 23:33
"""
import random,string
def rand_str(num,length = 7):
f = open("Activation_code2.txt","wb")
for i in range(num):
chars = string.ascii_letters + string.digits
s = [random.choice(chars) for i in range(length)]
f.write(bytes((''.join(s) + '\n' ), 'utf-8')) #f.write(''.join(s) + '\n') py2
f.close()
if __name__=="__main__":
rand_str(200)
会逐行写在文件里,涉及知识点:f.open f.writer random
#’str’ does not support the buffer interface 在python3 报错
with open("test.txt") as fp:
line = fp.readline()
with open("test.out", 'wb') as fp:
fp.write(line) #解决方案1 在Python3x中需要将str编码,
with open("test.txt") as fp:
line = fp.readline()
with open("test.out", 'wb') as fp:
fp.write(bytes(line, 'utf-8'))
#解决方案2 不想用b(binary)模式写入,那么用t(text, 此为写入的默认模式)模式写入可以避免这个错误.
with open("test.txt") as fp:
line = fp.readline()
with open("test.out", 'wt') as fp:
# with open("test.out", 'w') as fp:
fp.write(line)
随机验证码生成(python实现)的更多相关文章
- Python教程:随机验证码生成和join 字符串
函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符) ...
- 随机验证码生成和join 字符串
函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符) ...
- PYTHON 随机验证码生成
# 生成一个六位随机验证码 import random # random 生成随机数 temp = '' for i in range(6): num = random.randrange(0,6) ...
- python-内置函数-callable,chr,ord,bytes,随机验证码生成
s="老男人" bytes(s,encoding="utf-8") 随机验证码的实现方法: 大写字母: li = [] for i in range(6): t ...
- C#系统登录随机验证码生成及其调用方法
话不多说,直接上代码 public ValidateCode() { } /// <summary> /// 验证码的最大长度 /// </summary> public in ...
- python笔记-20 django进阶 (model与form、modelform对比,三种ajax方式的对比,随机验证码,kindeditor)
一.model深入 1.model的功能 1.1 创建数据库表 1.2 操作数据库表 1.3 数据库的增删改查操作 2.创建数据库表的单表操作 2.1 定义表对象 class xxx(models.M ...
- Python 生成随机验证码
Python生成随机验证码 Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 fro ...
- Python生成随机验证码
Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 基本使用 1.创建图片 from PIL import Image img = Image.new(m ...
- python模块之PIL模块(生成随机验证码图片)
PIL简介 什么是PIL PIL:是Python Image Library的缩写,图像处理的模块.主要的类包括Image,ImageFont,ImageDraw,ImageFilter PIL的导入 ...
随机推荐
- Maya 与 Matlab 数据互联插件使用教程
实验室做网格处理方面的算法,写界面很麻烦,所以有了利用maya和matlab进行数据连通的念头,于是有了这个插件. 这个插件可以把maya的网格数据导入matlab之中,完成计算之后重新返回maya. ...
- JSOI Round 2题解
强行一波题解骗一个访问量好了... http://blog.csdn.net/yanqval/article/details/51457302 http://absi2011.is-programme ...
- Spring JDBCTemplate使用JNDI数据源
xml配置: <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverMana ...
- Caffe学习系列(3):视觉层(Vision Layers)及参数
所有的层都具有的参数,如name, type, bottom, top和transform_param请参看我的前一篇文章:Caffe学习系列(2):数据层及参数 本文只讲解视觉层(Vision La ...
- Vue系列:如何将百度地图包装成Vue的组件
主要分解为如下步骤: (1)在html文件中引入百度地图, <script type="text/javascript" src="http://api.map.b ...
- 工作随笔——mysql子查询删除原表数据
最近在开发的时候遇到一个mysql的子查询删除原表数据的问题.在网上也看了很多方法,基本也是然并卵(不是写的太乱就是效率太慢). 公司DBA给了一个很好的解决方案,让人耳目一新. DELETE fb. ...
- FineUI v3.3.2发布!目前最稳定版本,五年陈酿!
关于FineUI基于 ExtJS 的专业 ASP.NET 控件库. FineUI的使命创建 No JavaScript,No CSS,No UpdatePanel,No ViewState,No We ...
- P和NP问题
1. 通俗详细地讲解什么是P和NP问题 http://blog.sciencenet.cn/blog-327757-531546.html NP----非定常多项式(英语:non-determin ...
- 贪吃蛇C#和JAVA实现
using System; using System.Windows.Forms; using System.Drawing; class Window : Form { Point[] a = ]; ...
- 【原创】解决jquery在ie中不能解析字符串类型xml结构的xml字符串的问题
$.fn.extend({ //此方法解决了ie中jquery不识别非xml的类型的xml字符串的问题 tony tan findX: function (name) { if (this & ...