1. 使用 uuid.uuid1 产生一个随机数
2. 在使用 random.sample() 产生一个随机字符串
3. 将两者进行拼接
import uuid
import random
# 1. random 生成一个 指定位数的随机字符串
strings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
random_str = random.sample(strings, 6)
# 参数 1 : str 要选择的 字符串
# 参数 2 : int 要获取的值
# 返回值 : list 返回的指定个数的 list
print("random_str: ", random_str)
# 2. 生成 uuid
# 从主机ID、序列号和当前时间生成UUID
# 保证全球唯一
uuid_value = uuid.uuid1()
uuid_str = uuid_value.hex # 将值转换成 字符串, 并且不添加 -
uuid_int = uuid_value.int # 返回生成的 int 值
print("uuid_value: ", uuid_value)
print(" uuid_str: ", uuid_str)
print(" uuid_int: ", uuid_int)
4. 结果
random_str: ['g', 'G', 'E', 'I', 'x', 'r']
uuid_value: 1ca91098-211c-11eb-adc6-acde48001122
uuid_str: 1ca91098211c11ebadc6acde48001122
uuid_int: 38096218622551330780224897817352605986