需求:生成随机不重复验证码。

代码:

#!/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实现)的更多相关文章

  1. Python教程:随机验证码生成和join 字符串

    函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符) ...

  2. 随机验证码生成和join 字符串

    函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符) ...

  3. PYTHON 随机验证码生成

    # 生成一个六位随机验证码 import random # random 生成随机数 temp = '' for i in range(6): num = random.randrange(0,6) ...

  4. python-内置函数-callable,chr,ord,bytes,随机验证码生成

    s="老男人" bytes(s,encoding="utf-8") 随机验证码的实现方法: 大写字母: li = [] for i in range(6): t ...

  5. C#系统登录随机验证码生成及其调用方法

    话不多说,直接上代码 public ValidateCode() { } /// <summary> /// 验证码的最大长度 /// </summary> public in ...

  6. python笔记-20 django进阶 (model与form、modelform对比,三种ajax方式的对比,随机验证码,kindeditor)

    一.model深入 1.model的功能 1.1 创建数据库表 1.2 操作数据库表 1.3 数据库的增删改查操作 2.创建数据库表的单表操作 2.1 定义表对象 class xxx(models.M ...

  7. Python 生成随机验证码

    Python生成随机验证码  Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 fro ...

  8. Python生成随机验证码

    Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 基本使用 1.创建图片 from PIL import Image img = Image.new(m ...

  9. python模块之PIL模块(生成随机验证码图片)

    PIL简介 什么是PIL PIL:是Python Image Library的缩写,图像处理的模块.主要的类包括Image,ImageFont,ImageDraw,ImageFilter PIL的导入 ...

随机推荐

  1. Oracle 使用MERGE INTO 语句更新数据

    /*Merge into 详细介绍MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句.通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询,连接条件匹配 ...

  2. nodeType的12种类型

    // NodeType const unsigned short ELEMENT_NODE = 1; 元素节点 const unsigned short ATTRIBUTE_NODE = 2; 属性节 ...

  3. 报错问题:InnoDB: Error: log file ./ib_logfile0 is of different size

    InnoDB: Error: log file ./ib_logfile0 is of different size bytesInnoDB: than specified in the .cnf f ...

  4. iOS原生项目中集成React Native

    1.本文的前提条件是,电脑上已经安装了CocoaPods,React Native相关环境. 2.使用Xcode新建一个工程.EmbedRNMeituan [图1] 3.使用CocoaPods安装Re ...

  5. 用Javascript判断访问来源操作系统, 设备, 浏览器类型

    var browser = { os : function() { var u = navigator.userAgent; return {// 操作系统 linux: !!u.match(/\(X ...

  6. 使用华为U8860测试时出现“Unable to open log device '/dev/log/main': No such file or directory”

    这是因为华为默认禁掉了log输出, 解决办法: 拨号: *#*#2846579#*#* 会显示工程菜单, Go to "ProjectMenu" -> "Backg ...

  7. php配置rewrite模块

    转 (1)    启用rewrite模块,在默认情况下,没有启用 修改httpd.conf文件 #启动rewrite模块 LoadModule rewrite_module modules/mod_r ...

  8. Java 集合系列06之 Vector详细介绍(源码解析)和使用示例

    概要 学完ArrayList和LinkedList之后,我们接着学习Vector.学习方式还是和之前一样,先对Vector有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它.第1部分 Vec ...

  9. Android 开发1000问笔记

    11.android使用全局变量 定义Data类继承Application 在manifest.xml中声明 http://blog.csdn.net/feiyangxiaomi/article/de ...

  10. 使用 Eclipse 调试 Java 程序的技巧

    你应该看过一些如<关于调试的N件事>这类很流行的帖子 .假设我每天花费1小时在调试我的应用程序上的话,那累积起来的话也是很大量的时间.由于这个原因,用这些时间来重视并了解所有使我们调试更方 ...