uuid模块是Python标准库的一部分,它提供了一种生成通用唯一识别码(Universally Unique Identifier,简称UUID)的方法,UUID是一种标识符标准,其目的在于提供一种全局范围内唯一的标识。
1 |
pip install uuid |
1 |
import uuid |
作用 基于MAC地址,时间戳,随机数来生成唯一的uuid,可保证全球范围的唯一性。
语法
1 |
uuid.uuid1(node=None, clock_seq=None) |
参数
示例
1 2 3 4 5 6 7 8 9 10 11 12 |
import uuid
# 生成一个基于时间的 UUID uuid1 = uuid.uuid1() print("UUID1:", uuid1)
# 如果你想要提供一个特定的 MAC 地址和时钟序列 custom_node = 0x123456789ABC # 举例用的 MAC 地址 custom_clock_seq = 0x1234 # 举例用的时钟序列
custom_uuid1 = uuid.uuid1(node=custom_node, clock_seq=custom_clock_seq) print("Custom UUID1:", custom_uuid1) |
结果
UUID1: f1667390-8dc0-11ef-ab64-fcb3bce2e1fe
Custom UUID1: f167111d-8dc0-11ef-9234-123456789abc
作用 通过计算命名空间和名字的MD5散列值来生成UUID, 可以保证同一命名空间中不同名字的唯一性和不同命名空间的唯一性, 但同一命名空间的同一名字生成的UUID相同。
语法
1 |
uuid.uuid3(namespace, name) |
参数
其他1.uuid命名空间:UUID名字空间是为了生成版本3和版本5 UUIDs时使用的一种机制。这些UUIDs是基于名字空间和一个名称(通常是字符串)通过散列函数计算得到的。名字空间UUIDs本身是一组预定义的UUIDs,它们为不同的命名空间提供唯一的标识符:
示例
1 2 3 4 5 6 7 8 9 |
import uuid
# 使用 DNS 名字空间 namespace = uuid.NAMESPACE_DNS name = 'python.org'
# 生成一个基于名字空间和名称的 UUID uuid3 = uuid.uuid3(namespace, name) print("UUID3:", uuid3) |
结果
UUID3: 6fa459ea-ee8a-3ca4-894e-db77e160355e
作用 生成版本4 UUIDs的函数,版本4 UUIDs是基于随机数(或者伪随机数)生成的,因此它们具有极高的唯一性,适用于需要唯一标识符的场合。
语法
1 |
uuid.uuid4() |
示例
1 2 3 4 5 6 |
import uuid
# 生成一个版本4的UUID random_uuid = uuid.uuid4()
print(f"Randomly generated UUID: {random_uuid}") |
结果
Randomly generated UUID: 5b38a29b-6a82-4d9b-812e-e030ad7afe4c
作用 生成版本5 UUIDs的函数,版本5 UUIDs是基于命名空间和一个名称生成的,使用SHA-1散列算法,这保证了只要命名空间和名称相同,生成的UUID就会相同。
语法
1 |
uuid.uuid5(namespace, name) |
参数
示例
1 2 3 4 5 6 7 8 9 10 |
import uuid
# 定义一个命名空间UUID(通常使用已知的UUID,如DNS或URL命名空间) namespace = uuid.UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8') # DNS命名空间 name = 'python.org' # 命名空间中的名称
# 生成一个版本5的UUID named_uuid = uuid.uuid5(namespace, name)
print(f"UUID for name '{name}' in namespace '{namespace}': {named_uuid}") |
结果
UUID for name 'python.org' in namespace '6ba7b810-9dad-11d1-80b4-00c04fd430c8': 886313e1-3b8a-5372-9b90-0c9aee199e5d
作用 获取当前机器的硬件地址(通常是MAC地址)并将其转换为整数值表示的节点ID。
语法
1 |
uuid.getnode() |
其他
示例
1 2 3 4 |
import uuid
node_id = uuid.getnode() print(node_id) |
结果
277848898331134
作用 生成UUID(Universally Unique Identifier,通用唯一识别码)。
语法
1 |
uuid.UUID(hex=None, bytes=None, bytes_le=None, fields=None, int=None, version=None) |
参数
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import uuid
# 从十六进制字符串创建 UUID uuid_from_hex = uuid.UUID('12345678-1234-5678-1234-567812345678') print(f"UUID from hex: {uuid_from_hex}")
# 从字节序列创建 UUID uuid_from_bytes = uuid.UUID(bytes=b'\x12\x34\x56\x78\x12\x34\x56\x78\x12\x34\x56\x78\x12\x34\x56\x78') print(f"UUID from bytes: {uuid_from_bytes}")
# 从整数创建 UUID uuid_from_int = uuid.UUID(int=0x12345678123456781234567812345678) print(f"UUID from int: {uuid_from_int}")
# 从字段创建 UUID uuid_from_fields = uuid.UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678)) print(f"UUID from fields: {uuid_from_fields}")
# 生成一个新的随机 UUID random_uuid = uuid.uuid4() print(f"Random UUID: {random_uuid}")
# 检查 UUID 版本 if random_uuid.version == 4: print("This is a random UUID.") |
结果
UUID from hex: 12345678-1234-5678-1234-567812345678
UUID from bytes: 12345678-1234-5678-1234-567812345678
UUID from int: 12345678-1234-5678-1234-567812345678
UUID from fields: 12345678-1234-5678-1234-567812345678
Random UUID: 2e9a79ae-cc0a-4eac-8e2a-102fa9237c57
This is a random UUID