There are many ways to generate random verification code using Python, today's small series to share two methods, we can flexibly use these two methods to design a suitable verification code method.
Method One:
Use the Range method, for students who are not clear about the range method, please refer to the article "range () function developed by Python"
#-*-Coding:utf-8-*-import randomdef generate_verification_code (len=6): ' Randomly generate 6-bit Verification code ' # Note: Here we generate a list of 0-9a-za-z, when You can also specify this list, which is very flexible # for example: Code_list = [' P ', ' y ', ' t ', ' h ', ' o ', ' n ', ' t ', ' a ', ' B '] # Pythontab's Letter code_list = [] For I in Ran GE (10): # 0-9 Digital code_list.append (str (i)) for I in range (65, 91): # ASCII code from "a" to "Z" code_list.append (Chr (i)) For I in range (123): ASCII code for #对应从 "a" to "Z" code_list.append (Chr (i)) Myslice = Random.sample (Code_list, Len) # from List Randomly gets 6 elements, returning as a fragment Verification_code = '. Join (myslice) # List to string return Verification_code
Method Two:
Using the Randint method
#-*-Coding:utf-8-*-import randomdef generate_verification_code_v2 (): "' randomly generate 6-bit captcha ' code_list = [] for I in range ( 2): random_num = random.randint (0, 9) # randomly generates 0-9 numbers # Use the Random.randint () function to generate a random integer A, which makes the 65<=a<=90 # For ASCII code from "a" to "Z" a = Random.randint (+) B = Random.randint (122) random_uppercase_letter = Chr (A) C6/>random_lowercase_letter = Chr (b) code_list.append (str (random_num)) Code_list.append (random_uppercase _letter) 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, more flexible, can be arbitrarily set the length of the verification code.
Python randomly generates Chinese verification code
#-*-coding:utf-8-*-import image,imagedraw,imagefont import random import Math, String Class Randomchar (): "" "for randomly generated Han Word "" "@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 = (+, +), Fontpath = ' WQY.TTC ', BgColor = (255, 255, 255), fontSize =): Self.siz E = Size Self.fontpath = Fontpath Self.bgcolor = BgColor Self.fontsize = fontSize Self.fontcolor = FontColor Self.fon t = Imagefont.truetype (Self.fontpath, self.fontsize) self.image = image.new (' RGB ', size, bgColor) def rotate (self): sel F.image.rotate (random.randint (0), 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)), Ra Ndomchar (). GB2312 (), Self.randrgb ()) self.rotate () Self.randline def save (Self, Path): Self.image.save (Path)