zoukankan      html  css  js  c++  java
  • 随机生成验证码的两种方法

    方法一:

    利用range方法

    import random

    def generate_verification_code(len=6):

        ''' 随机生成6位的验证码 '''

        # 注意: 这里我们生成的是0-9A-Za-z的列表,当然你也可以指定这个list,这里很灵活

        # 比如: code_list = ['P','y','t','h','o','n','T','a','b'] # PythonTab的字母

        code_list = [] 

        for i in range(10): # 0-9数字

            code_list.append(str(i))

        for i in range(65, 91): # 对应从“A”到“Z”的ASCII码

            code_list.append(chr(i))

        for i in range(97, 123): #对应从“a”到“z”的ASCII码

            code_list.append(chr(i))

        myslice = random.sample(code_list, len)  # 从list中随机获取6个元素,作为一个片断返回

        verification_code = ''.join(myslice) # list to string

        return verification_code

    方法二:

    利用randint方法

    import random

    def generate_verification_code_v2():

        ''' 随机生成6位的验证码 '''

        code_list = []

        for i in range(2):

            random_num = random.randint(0, 9) # 随机生成0-9的数字

            # 利用random.randint()函数生成一个随机整数a,使得65<=a<=90

            # 对应从“A”到“Z”的ASCII码

            a = random.randint(65, 90)

            b = random.randint(97, 122)

            random_uppercase_letter = chr(a)

            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

  • 相关阅读:
    poj 3616 Milking Time
    poj 3176 Cow Bowling
    poj 2229 Sumsets
    poj 2385 Apple Catching
    poj 3280 Cheapest Palindrome
    hdu 1530 Maximum Clique
    hdu 1102 Constructing Roads
    codeforces 592B The Monster and the Squirrel
    CDOJ 1221 Ancient Go
    hdu 1151 Air Raid(二分图最小路径覆盖)
  • 原文地址:https://www.cnblogs.com/-simon/p/5887588.html
Copyright © 2011-2022 走看看