zoukankan      html  css  js  c++  java
  • re随机模块应用-生成验证码(无图片)

    方法一,通过choice方式生成验证码

    此方法生成每次调用crate_code()会生成三个随机数,然后再三个随机数中选择一个,资源调用相对多些

    import random
    
    def v_code(code_length):
        res = []
        if isinstance(code_length,int):
            for i in range(code_length):
                ret = create_code()
                res.append(ret)
            return res
        else:
            print("请以数字形式输入多少位")
    
    
    def create_code():   #生成随机验证码,通过随机choice的方式
        num = chr(random.randint(48, 57)) #随机数字
        alfs = chr(random.randint(65, 90)) #随机大写字母
        alfb = chr(random.randint(97, 122)) #随机大写字母
        s = str(random.choice([num,alfs,alfb]))
        return s
        #方法二,通过指定一个choice的方式指定
        # choice = random.randint(1,3)
        # if choice == 1:
        #     return chr(random.randint(48, 57)) #随机数字
        # elif choice ==2:
        #     return chr(random.randint(65, 90)) #随机大写字母
        # elif choice ==3:
        #     return chr(random.randint(97, 122)) #随机小写字母
    
    if __name__ == "__main__":
        code = v_code(4)
        for i in range(4):
            code[i] = str(code[i])
        code_str =  ''.join(code)
        print("数组类型的展示为:",code)
        print("转换成str类型后:",code_str)
    

    方法二,通过random生成choice

    此根据choice方式一次生成一次随机数

    import random
    
    def v_code(code_length):
        res = []
        if isinstance(code_length,int):
            for i in range(code_length):
                ret = create_code()
                res.append(ret)
            return res
        else:
            print("请以数字形式输入多少位")
    
    
    def create_code():   #生成随机验证码,通过随机choice的方式
        # num = chr(random.randint(48, 57)) #随机数字
        # alfs = chr(random.randint(65, 90)) #随机大写字母
        # alfb = chr(random.randint(97, 122)) #随机大写字母
        # s = str(random.choice([num,alfs,alfb]))
        # return s
        #方法二,通过指定一个choice的方式指定
        choice = random.randint(1,3)
        if choice == 1:
            return chr(random.randint(48, 57)) #随机数字
        elif choice ==2:
            return chr(random.randint(65, 90)) #随机大写字母
        elif choice ==3:
            return chr(random.randint(97, 122)) #随机小写字母
    
    if __name__ == "__main__":
        code = v_code(4)
        for i in range(4):
            code[i] = str(code[i])
        code_str =  ''.join(code)
        print("数组类型的展示为:",code)
        print("转换成str类型后:",code_str)
    
  • 相关阅读:
    解决Windows2003不能自动分配移动存储设备及硬盘盘符
    Asp.net2.0工具包AjaxControlToolkit下载和安装
    VC++开发MapX
    MapXtreme 中改变feature颜色
    IIS 发布网站流程
    net 2.0下的asp.net ajax基本使用方法
    vs2005下,"回发或回调参数无效"的解决方法
    MapXtreme 2005 地图标注全攻略
    Mapxtreme符号化
    MapXtreme 使用技巧10例
  • 原文地址:https://www.cnblogs.com/chrrydot/p/9800889.html
Copyright © 2011-2022 走看看