Updated March 31, 2023
Introduction to Python UUID
In this article, we will discuss Python UUID which is a Python module used for implementing or generating the universally unique identifiers and is also known as GUID globally unique identifiers. Python UUID module generates the identifiers randomly which have the value of 128 bit long and these identifiers are useful for documents or information in computer systems, apps, hosts, and many different situations that will use unique identifiers. This Python UUID module provides different immutable Objects and different versions of functions such as uuid1(), uuid3(), uuid4(), uuid5() which are used for generating UUID’s of versions 1, 3, 4, and 5.
Working of Python UUID with Examples
In Python, there is a library or module which is inbuilt and is used for generating unique identifiers that are universal or global and this module is known as UUID, this module can also generate different versions of UUIDs and these modules are immutable which means their value cannot be altered once generated. UUID is mainly composed of 5 components with fixed lengths and each component is separated by a hyphen and uses read attributes to define the UUID string. This Python UUID is implemented based on RFC 4211 which includes different algorithms and information regarding the unique identifiers that are to be generated along with the required versions of UUIDs. In Python, this module provides various functions for different versions such as uuid1(), uuid3(), uuid4() and uuid5().
In Python, the UUID module provides various read-only attributes such as:
- UUID.bytes which includes a 16-byte string.
- UUID.fields which includes fields like time, clock_seq, node, etc.
- UUID.hex can hold the 32-bit hexadecimal string.
- UUID.int can hold 128-bit integer
- UUID.Safe this attribute tells us the uuid version used is safe or not.
Examples of Python UUID
In the below section let us see a few examples of the use of function uuid1(), uuid3(), uuid4() and uuid5() using Python UUID module which is mainly used for generating UUID using MAC address. We will also see how the UUID looks like which means the structure of UUID.
But we should note that when using uuid1() it might display network details such as the network address of the computer in UUID so it is not so safe to use uuid1() as it may arise privacy problems because it uses the systems MAC address. Let us see a simple example.
import uuid print("Progam to demonstrate uuid1() function:") print("\n") uuid_version_1 = uuid.uuid1() print("UUID of version one is as follows", uuid_version_1)
In the above program, we can see the uuid1() function is used which generates the host id, the sequence number is displayed. We can compute these function values using the MAC address of the host and this can be done using the getnode() method of UUID module which will display the MAC value of a given system. Say for example
The above statement will output the MAC value in hexadecimal value and if this getnode() method fails to display the mAC address then it will by default return a 48-bit number along with the multicast bit. So as we saw uuid1() is not safe to use and can compromise privacy and now we will see uuid4() which will not compromise privacy as it uses a pseudo-random number generator to generate unique identifiers. Let us see a simple example of implementing this uuid4() function.
import uuid print("Program to demonstrate uuid4() function:") print("\n") unique_id = uuid.uuid4() print ("The unique id generated using uuid4() function : ") print (unique_id)
In the above program, we can see a unique id is generated using uuid4(). The uuid4() generates id using cryptographically secure random number generators hence there is less chance of collision.
Now we will see uuid3() and uuid5() where we saw a generation of UUID using random numbers now we will see how to generate UUIDs using names instead of random numbers using uuid3() and uuid5() which uses cryptographic hash values such as MD5 or SHA-1 to combine values with the names like hostnames, URLs, etc. In general, uuid3() and uuid5() versions are hashing namespace identifiers with a name, and few namespaces are defined by UUID module such as UUID.NAmESPACE_DNS, UUID.NAmESPACE_URL, etc. Now let us see an example below.
import uuid print("Program to demonstrate uuid3() and uuid5() is as follows:") print("\n") hosts_sample = ('www.educba.com', 'www.google.com') for hostname in hosts_sample: print("Hostname specified is as follows: ",hostname) print('\tThe SHA-1 value of the given hostname:', uuid.uuid5(uuid.NAMESPACE_DNS, hostname)) print('\tThe MD5 value of the given hostname :', uuid.uuid3(uuid.NAMESPACE_DNS, hostname)) print("\n")
In the above program, we can see we are using uuid3() and uuid5() functions which generate UUID at different times but with the same namespace and same name. In the above program, we have two different hostnames and we are iterating using for loop. We can specify any number of hostnames and can iterate it using for loop.
As UUID is a unique universal identifier there are some privacy issues as we saw in the above section uuid1() compromises with privacy as it uses systems MAC address whereas uuid4() doesn’t compromise with privacy hence it uses a random number generator for generating UUIDs. Therefore we can say uuid1() is not safe to use and uuid4() is safer than uuid1(). Therefore to check if the UUID functions are safe in the latest Python version 3.7 an instance of UUID such as is_safe attribute is used to check for UUID is safe or not. UUIDs are used in various applications such as in web apps, database systems, etc. In Python, we can convert UUID to string and vice versa using str class and we can obtain string format removing the hyphen that is used for separation of components in UUID using string method replace() by replacing “-” with “” say for example
UUID_id = uuid.uuid1() str_uuid = str(UUID_id). replace("-", "")
And similarly, we can convert the string back to UUID using UUID instance such as follows:
uuid_id = uuid.UUID(string)
In this article, we conclude that UUID is a unique universal identifier and is also known as a global identifier. In this article, we also saw the Python UUID module to generate the identifiers using a few uuid functions of different versions and we also saw different uuid() versions such as uuid1(), uuid3(), uuid4(), and uuid5() with examples and their privacy terms. In this, we also saw different read attributes, safety checks for uuid() function, and also saw the conversion of UUID to string and vice versa.
This is a guide to Python UUID. Here we also discuss the introduction and working of python uuid along with different examples and its code implementation. You may also have a look at the following articles to learn more –