python's random module can be used to generate N-bit verification code randomly, and the principle is simple.
1 ImportRandom2 #Complete generation of 5-bit random verification codes3 defvaliate1 ():4String ="'5 forIinchRange (5):#set the number of verification code bits6Rand_num = Random.randint (0,61)#there are altogether 62 possible cases.7 ifRand_num < 10:8string + =Str (rand_num)9 elifTen <= Rand_num <= 35:Tenstring + = Chr (rand_num+55)#randomly generated A-Z One Else: Astring + = Chr (rand_num+61)#randomly generated a-z3 - returnstring - Print(Valiate1 ()) the #the method numbers, uppercase letters, lowercase letters appear the same probability - defvaliate2 (): -String ="' - forIinchRange (5):#set the number of verification code bits +Rand_num = Random.randint (0,9) -Rand_alpha = Chr (Random.randint (97,122)) +Rand_alpha = Chr (Random.randint (65,90)) Ares =Random.choice ([Str (rand_num), Rand_alpha,rand_alpha]) atstring + =Res - returnstring - Print(Valiate2 ())
Just a few lines of code will be able to solve our written login interface related program validation module, easy to use!
Note: The parameters in Random.choice () must be in the form of a list.
Python implements a simple Captcha module