>>> import uuid
>>> # make a random UUID>>> uuid.uuid4()UUID('bd65600d-8669-4903-8a14-af88203add38')
>>> # Convert a UUID to a string of hex digits in standard form>>> str(uuid.uuid4())'f50ec0b7-f960-400d-91f0-c42a6d44e3d0'
>>> # Convert a UUID to a 32-character hexadecimal string>>> uuid.uuid4().hex'9fe2c4e93f654fdbb24c02b15259716c'
>>> import uuid
>>> # make a UUID based on the host ID and current time>>> uuid.uuid1()UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')
>>> # make a UUID using an MD5 hash of a namespace UUID and a name>>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')
>>> # make a random UUID>>> uuid.uuid4()UUID('16fd2706-8baf-433b-82eb-8c7fada847da')
>>> # make a UUID using a SHA-1 hash of a namespace UUID and a name>>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')
>>> # make a UUID from a string of hex digits (braces and hyphens ignored)>>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')
>>> # convert a UUID to a string of hex digits in standard form>>> str(x)'00010203-0405-0607-0809-0a0b0c0d0e0f'
>>> # get the raw 16 bytes of the UUID>>> x.bytes'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'
>>> # make a UUID from a 16-byte string>>> uuid.UUID(bytes=x.bytes)UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')
import random as r
def generate_uuid():random_string = ''random_str_seq = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"uuid_format = [8, 4, 4, 4, 12]for n in uuid_format:for i in range(0,n):random_string += str(random_str_seq[r.randint(0, len(random_str_seq) - 1)])if n != 12:random_string += '-'return random_string
from typing import Optionalimport reimport subprocessimport uuid
def get_windows_uuid() -> Optional[uuid.UUID]:try:# Ask Windows for the device's permanent UUID. Throws if command missing/fails.txt = subprocess.check_output("wmic csproduct get uuid").decode()
# Attempt to extract the UUID from the command's result.match = re.search(r"\bUUID\b[\s\r\n]+([^\s\r\n]+)", txt)if match is not None:txt = match.group(1)if txt is not None:# Remove the surrounding whitespace (newlines, space, etc)# and useless dashes etc, by only keeping hex (0-9 A-F) chars.txt = re.sub(r"[^0-9A-Fa-f]+", "", txt)
# Ensure we have exactly 32 characters (16 bytes).if len(txt) == 32:return uuid.UUID(txt)except:pass # Silence subprocess exception.
return None
print(get_windows_uuid())
>>> import uuid, uuid6>>> print(*(str(i()) for i in [uuid.uuid1, uuid.uuid4, uuid6.uuid6, uuid6.uuid7]), sep="\n")646e934b-f20c-11ec-ad9f-54a1500ef01b560e2227-c738-41d9-ad5a-bbed6a3bc2731ecf20b6-46e9-634b-9e48-b2b9e6010c5701818aa2-ec45-74e8-1f85-9d74e4846897