Python real-Battle series generated random verification code (03)

Source: Internet
Author: User
Tags python list

background : In some landing sites, after entering the user name and password, usually also need to enter a verification code, the verification code can be used to encrypt the salt, to prevent some malicious attacks, the following through Python to generate arbitrary length random verification code, verification Code capital letters, lowercase letters and numbers, Where lowercase letters from 97 to 122 of the ASIIC code, the size of the letter 65 to 90 composition, through the Chr () function, the ASIIC code into letters, as follows through a few lines of code can be achieved.


Program Content :

#!/usr/bin/env python#_*_ coding:utf8 _*_#author:happy# from the Happy lab, which is used to randomly generate an arbitrary length verification code import  randomdef generate_vertification_code (number):         "          generate num bits, where you generate a list of verification codes using the serialization of the list, and then read through the random module           '         list_sample = [str ( num)  for num in range (0,10)] + [chr (num)  for num in range ( 65,91)] + [CHR (num)  for num in range (97,123)]         vertification_code =  "". Join (Random.sample (list_sample,number))          return vertification_code        if  __name__ ==  "__main__":         print generate_ Vertification_code (4)          test Result: python generate_vertification_code.py deaf 


Extended Knowledge : The above example generates a list of verification codes in the form of a Python list, which is difficult for a classmate to see, and the following example generates a list of verification codes by looping through iterations

#!/usr/bin/env python#_*_ coding:utf8 _*_# from Happy Lab Import randomdef gen_code (number) :        code_list = []          '          generate numbers and convert to string type           '         for num in range (0,10):                 code_ List.append (num)          '           uppercase letters, converting ASSIC codes to letters          '          for lower in range (65,91):                 code_list.append (Chr (lower))           '   &Nbsp;      lowercase letters, converting assic codes to letters          '          for upper in range (97,123):                 code_list.append (Chr (upper))           '          Select elements of any length and join () function to concatenate elements together          ' '          vertification_code =  "". Join (Random.sample (code_list,number))          return vertification_codeif __name__ ==  "__main__":         print gen_code (4)


Additional knowledge : The random module has some other uses

>>> import random>>> random.random ()           #生成随浮点数的随机数0.86806950084542456>>> random.randint (0,10)     #生成整形随机数, You can define a range 3>>> random.randrange (0,1)     #和randint () method is similar, Randrange () is a closed space, That is, the last element excludes 0# other methods >>> dir (random) [' BPF ',  ' LOG4 ',  ' nv_magicconst ',  ',   ' Random ',  ' sg_magicconst ',  ' systemrandom ',  ' Twopi ',  ' Wichmannhill ',  ' _ Builtinmethodtype ',  ' _methodtype ',  ' __all__ ',  ', __builtins__ ',  ' __doc__ ',  ' __file__ ',   ' __name__ ',  ' __package__ ',  ' _acos ',  ' _ceil ',  ' _cos ',  ' _e ',  ' _exp ',  ' _ Hexlify ',  ' _inst ',  ' _log ',  ' _pi ',  ', ' _random ',  ' _sin ',,  ' _sqrt ',  ' _test ',   ' _test_generator ',  ' _urandom ',  ' _warn ',  ' betavariate ',  ' Choice ',  ' division ',   ' expovariate ',  ' gammavariate ',  'Gauss ',  ' getrandbits ',  ' getstate ',  ', Jumpahead ',  ' lognormvariate ',  ' normalvariate ',   ' paretovariate ',  ' randint ',  ' random ',  ' randrange ',  ' sample ',  ' seed ',  ' SetState ',  ' shuffle ',  ' triangular ',  "uniform ',  ' vonmisesvariate ',  ' weibullvariate ']



This article is from the "Happy Lab" blog, so be sure to keep this source http://happylab.blog.51cto.com/1730296/1731010

Python real-Battle series generated random verification code (03)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.