Python生成验证码

#!/usr/bin/env python

#coding:utf8

import random

#方法1:

str_code='zxcvbnmasdfghjklqwertyuiopZXCVBNMASDFGHJKLQWERTYUIOP0123456789'
new_code=''
for i in range(4):
  new_code+=random.choice(str_code)
print new_code

#方法2:

new_code=[]
def str_code(len=4):
  for i in xrange(97,123):
    new_code.append(chr(i))
  for i in xrange(65,91):
    new_code.append(chr(i))
  for i in xrange(10):
    new_code.append(str(i))
  print ''.join(random.sample(new_code,len))

str_code()

#方法3:

def code(randomlength=32):
  str = ""
  chars = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789"
  length = len(chars) - 1
  for i in range(randomlength):
    str += chars[random.randint(0, length)]
  return str

code()

上一篇:mysql忘记密码修改方法


下一篇:MySQL基础学习之数据库