python3 随机生成验证码\字符串\密码\UUID

随机生成验证码\字符串\密码\UUID

数字验证码

import random
import uuid
def check_num():
    check_num = ''
    for i in range(4):
        current = random.randint(1, 9)
        check_num += str(current)
    print(check_num)
check_num()

随机字母+数字验证码

import random
import uuid

def check_code():
    check_code = ''
    for i in range(4):
        current = random.randrange(0, 4)
        if current == i:
            tmp = chr(random.randint(65, 90))
        else:
            tmp = random.randint(0, 9)
        check_code += str(tmp)
    print(check_code)
check_code()

随机生成密码

import random
import uuid

def password():
    Maclist = []
    for i in range(1, 6):
        RANDSTR = "".join(random.sample("0123456789abcdef", 2))
        Maclist.append(RANDSTR)
    RANDMAC = "".join(Maclist)
    # print(RANDMAC.upper())
    return RANDMAC
print(password())

随机生成uuid

import random
import uuid

def uuid_random():
    # uuid.uuid1():基于时间戳
    uuid_str1= uuid.uuid1()
    print("uuid_str1=", uuid_str1)
    # 使用uuid3产生基于名字的MD5散列值
    uuid3_str3 = uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
    print("uuid3_str3=",uuid3_str3)
    uuid3_str3_3 = str(uuid.uuid3(uuid.NAMESPACE_DNS, "username")).replace("-", "")
    print("uuid3_str3_3=",uuid3_str3_3)
    # 使用uuid4产生32位随机字母加数字
    uuid_str4 = str(uuid.uuid4())
    uuid_str4_4 = str(uuid.uuid4()).replace("-", "")
    print("uuid_str4=",uuid_str4)
    print("uuid_str4_4=",uuid_str4_4)

    #基于名字的SHA-1散列值
    uuid5_str5 = uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
    print("uuid5_str5 =", uuid5_str5)
uuid_random()

生成随机数字字符串,param length: 字符串长

import random
import uuid

def get_random_number_str(length):
    num_str = ''.join(str(random.choice(range(10))) for _ in range(length))
    return num_str
length= 6
print(get_random_number_str(length))

随机生成6位字符串

import random

 pwd =  "".join(random.sample("0123efghjkl456mnopqstuvwsz789abcdef", 6))
 print(pwd)
posted @ 2020-03-09 16:28  莫贞俊晗  阅读(846)  评论(0编辑  收藏  举报