There are many ways to generate a Random verification code using python. Today I will share two methods for you to use them flexibly, there are many methods to generate a Random verification code using python. Today I will share with you two methods. You can use these two methods flexibly, design a suitable verification code method.
Method 1:
For details about the range method, see the range () function developed by python.
#-*-Coding: UTF-8-*-import randomdef generate_verification_code (len = 6): ''' a 6-digit verification code is randomly generated ''' # Note: here we generate a list of 0-9A-Za-z. Of course, you can also specify this list, which is flexible # For example: code_list = ['P', 'y','t ', 'H', 'O', 'n', 't', 'A', 'B'] # The PythonTab letter code_list = [] for I in range (10 ): #0-9 digit code_list.append (str (I) for I in range (65, 91): # Corresponds to the ASCII code code_list.append (chr (I) from "A" to "Z )) for I in range (97,123): # Corresponds to the ASCII code code_list.append (chr (I) from "a" to "z" myslice = random. sample (code_list, len) # randomly obtain 6 elements from the list and return verification_code = ''as a piece ''. join (myslice) # list to string return verification_code
Method 2:
Use the randint Method
#-*-Coding: UTF-8-*-import randomdef generate_verification_code_v2 (): ''' a 6-digit verification code is randomly generated ''' code_list = [] for I in range (2 ): random_num = random. randint (0, 9) # randomly generate 0-9 numbers # use random. the randint () function generates a random integer a so that 65 <= A <= 90 # Corresponds to the ASCII code a = random from "a" to "Z. randint (65, 90) B = random. randint (97,122) Comment = chr (a) Comment = chr (B) code_list.append (str (random_num) code_list.append (comment) code_list.append (random_lowercase_letter) verification_code = ''. join (code_list) return verification_code
Test:
Code = generate_verification_code (6)
Code2 = generate_verification_code_v2 ()
Print code
Print code2
Output result:
Glc5Tr
Hr6t7B
I personally prefer the first method, which is more flexible and can set the verification code length at will.
Python randomly generates a Chinese Verification Code
#-*-Coding: UTF-8-*-import Image, ImageDraw, ImageFont import random import math, string class RandomChar (): "is used to randomly generate Chinese characters" @ staticmethod def Unicode (): val = random. randint (0x4E00, 0x9FBF) return unichr (val) @ staticmethod def GB2312 (): head = random. randint (0xB0, 0xCF) body = random. randint (0xA, 0xF) tail = random. randint (0, 0xF) val = (head <8) | (body <4) | tail str = "% x" % val return str. decode ('hex '). decode ('gb2312') class ImageChar (): def _ init _ (self, fontColor = (0, 0, 0), size = (100, 40 ), fontPath = 'wqy. ttc ', bgColor = (255,255,255), fontSize = 20): self. size = size self. fontPath = fontPath self. bgColor = bgColor self. fontSize = fontSize self. fontColor = fontColor self. font = ImageFont. truetype (self. fontPath, self. fontSize) self. image = Image. new ('rgb ', size, bgColor) def rotate (self): self. image. rotate (random. randint (0, 30), expand = 0) def drawText (self, pos, txt, fill): draw = ImageDraw. draw (self. image) draw. text (pos, txt, font = self. font, fill = fill) del draw def randRGB (self): return (random. randint (0,255), random. randint (0,255), random. randint (0,255) def randPoint (self): (width, height) = self. size return (random. randint (0, width), random. randint (0, height) def randLine (self, num): draw = ImageDraw. draw (self. image) for I in range (0, num): draw. line ([self. randPoint (), self. randPoint ()], self. randRGB () del draw def randChinese (self, num): gap = 5 start = 0 for I in range (0, num): char = RandomChar (). GB2312 () x = start + self. fontSize * I + random. randint (0, gap) + gap * I self. drawText (x, random. randint (-5, 5), RandomChar (). GB2312 (), self. randRGB () self. rotate () self. randLine (18) def save (self, path): self. image. save (path)