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

代码:

#!/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. windows live Writer test

    package com.newegg.shopping.util.listener; import javax.servlet.http.HttpSessionAttributeListener; i ...

  2. gradient 线性渐变 浏览器兼容

    filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0, startColorstr=white, endColorstr= ...

  3. 用opencv的traincascade训练检测器

    #1,准备正负样本 正样本:可以一张图片上多个sample,也可以一张图片单独成一个sample,准备多个sample.生成描述文件如下所示: 负样本:只要不含正样本,任意图片都可以作为负样本,但是最 ...

  4. .NET:Entity Framework 笔记

    有二年没关注EF,今天无意试了下发现跟主流的Hibernate等ORM框架越来越接近了,先看下Entity类的定义: using System; using System.Collections.Ge ...

  5. 求最长回文子串 - leetcode 5. Longest Palindromic Substring

    写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...

  6. Bootstrap系列 -- 41. 带表单的导航条

    有的导航条中会带有搜索表单,在Bootstrap框架中提供了一个“navbar-form”,使用方法很简单,在navbar容器中放置一个带有navbar-form类名的表单.navbar-left”让 ...

  7. node简单操作mysql的类

    Creative.js 'use strict'; var pool = require('../utils/MysqlUtils'); var util = require('util'); var ...

  8. AutoMapperHelper

    /// <summary> /// AutoMapper帮助类 /// </summary> public static class AutoMapperHelper { // ...

  9. exgcd,求乘法逆元

    procedure exgcd(a,b:int64); var t:longint; begin then begin x:=;y:=; exit; end else exgcd(b,a mod b) ...

  10. team foundation server——网络代码管理工具

    像我们平时有时会莫名的弹出一个如下图所示的提示框,这个是什么呢?这个就是有人用team foundation server进行过代码管理的项目 那么team foundation server到底是什 ...