Python--uuid

Source: Internet
Author: User
Tags md5 hash rfc

The UUID module is introduced after Python 2.5, which includes: Immutable object UUID (UUID Class) and Functions Uuid1 (), UUID3 (), Uuid4 (), and UUID5 (), followed by four functions for generating 1th, 3, 4, specified in the RFC 4122 specification Version 5 uuid. Use UUID1 () or UUID4 () to obtain a unique ID,UUID1 () that contains the network name of the host, Uuid4 () does not involve a network hostname, generates only a random uuid, and is therefore more secure from a privacy-protection perspective Uuid4 ().

class uuid. UUID ([hex[, bytes[, bytes_le[, fields[, int[, Version]] [ ]]

This class is used to instantiate a UUID object (hex, Bytes, bytes_le, fields, int must and can only be specified) from the given content of the parameter:

  Hex: Creates a UUID object in 32 16 characters, which are optional when specifying a 32-character string to create a UUID object, such as curly braces, hyphens, and urn prefixes;

  bytes: Specifies a total length of 16 bytes of byte string to create the UUID object;

  Bytes_le: Specifies a small-endian 16-byte string to create a UUID object;

  fields: The UUID is created from 6 integer domains totaling 128 bits (32 bits as Time_low segment, 16 bits as Time_mid segment, 16 bits as time_hi_version segment, 8 bits as Clock_seq_hi_ Variant segment, 8 bits as Clock_seq_low segment, 48 bits as node segment);

  int: Directly specifying an integer length of 128 bits to create a UUID object;

  version: (optional) Specify the versions of the UUID, from 1 to 5, once this parameter is specified, the resulting UUID will have its own variant (variant) and version number, please refer to RFC 4122,

Cases.

The following various methods create the same UUID object,

u = uuid (' {12345678-1234-5678-1234-567812345678} ') U = uuid (hex = ' 12345678123456781234567812345678 ') u = UUID (' Urn:uuid : 12345678-1234-5678-1234-567812345678 ') u = uuid (bytes= ' \x12\x34\x56\x78 ') u = uuid (bytes_le= ' \x78\x56\x34\x12\x34 \x12\x78\x56 ' +              ' \x12\x34\x56\x78\x12\x34\x56\x78 ') u = UUID (fields= (0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678)) U = UUID (int=0x12345678123456781234567812345678)

The UUID object contains the following read-only properties

uuid.bytes

  A UUID in the form of a 16-byte string that contains 6 integer fields of the big endian byte sequence;

>>> u.bytes ' \X124VX\X124VX\X124VX\X124VX '

Uuid.bytes_le

A UUID in the form of a 16-byte string that contains 6 integer fields of small endian bytes;

>>> u.bytes_le ' XV4\X124\X12XV\X124VX\X124VX '

  

Uuid.fields
6 integer fields of uuid stored in tuples, 6 elements of which can be viewed by 6 properties, and two additional attributes (the name of each of the following fields is also a property of the UUID object):
Domain Meaning
Time_low Initial 32-bit of UUID
Time_mid 16 bits from the previous field
Time_hi_version 16 bits from the previous field
Clock_seq_hi_variant 8 bits from the previous field
Clock_seq_low 8 bits from the previous field
Node Last 48 digits of the UUID
Time Timestamp of the total length of the UUID 60 bits
Clock_seq 14-bit serial number
>>> U.fields (305419896L, 4660L, 22136L, 18L, 52L, 95073701484152L)
Uuid.hex
UUID in 32-character notation
>>> U.hex ' 12345678123456781234567812345678 '
  
Uuid.int
A UUID that is represented by an integer length of 128 bits;
>>> u.int24197857161011715162171839636988778104l
  
Uuid.urn
A UUID in the form of a urn specified in RFC 4122;
>>> U.urn ' urn:uuid:12345678-1234-5678-1234-567812345678 '
  
uuid.variant
The UUID Variant (variant), which determines the layout within the UUID, and the existing values are Reserved_ncs,rfc_4122,reserved_microsoft , or Reserved_future;
>>> u.variant ' reserved for NCS compatibility '
  
uuid.version
The UUID version, which is valid only if the variant is rfc_4122.
>>> u.version>>>

Here u.version is empty because u.variant = = ' reserved for NCS compatibility '.

  
The UUID module also defines the following functions
Uuid.getnode ()
   Gets the address of the hardware and returns it as a positive integer of 48-bit binary length, where the hardware address refers to the MAC address of the network interface, and if a machine has multiple network interfaces, it may return either. If the acquisition fails, the 8th bit of the randomly returned 48-bit binary integer is set to 1, as specified in RFC 4122.
>>> Uuid.getnode () 152667293855L
  
UUID.UUID1 ([node[, Clock_seq]])
generates a UUID using the host ID, serial number, and current time, if the parameter node If not given, GetNode () is called to obtain the hardware address. If clock_seq is specified in the parameter, the sequence of clocks given in the parameter is used as the serial number, otherwise a random 14-bit long sequence number is used.
>>> uuid.uuid1 () uuid (' a89e9d00-a710-11e4-a84a-00238bae089f ')
  
Uuid.uuid3 (namespace, name)
The UUID is generated based on the MD5 hash of the namespace identifier (essentially a UUID) and a name (essentially a string).
  
Uuid.uuid4 ()
   Generate a random UUID
>>> Uuid4 () UUID (' b9f9fb88-49f3-4cea-9885-19e57c3572c6 ')
  
Uuid.uuid5 (namespace, name)
   Generates a UUID based on a SHA-1 hash of the namespace identifier (essentially a UUID) and a name (essentially a string)
  
For the namespace identifiers mentioned in Uuid3 () and UUID5 (), the UUID module defines the following preparation options
UUID. Namespace_dns
When the namespace is specified, the parameter name is a fully qualified (fully-qualified) domain name
  
UUID. Namespace_url
When you specify the namespace, the parameter name is a URL
  
UUID. Namespace_oid
  When the namespace is specified, the parameter name is an ISO OID
  
UUID. namespace_x500
When the namespace is specified, the parameter name is a X.500 DN of the DER format or text format.
  
About the Properties Variant,uuid module defines the following constants
  
UUID. Reserved_ncs
The constant is reserved for compatibility with NCS;
  
UUID. rfc_4122
   to determine the layout of the UUID in accordance with the provisions of RFC 4122 ;
  
UUID. Reserved_microsoft
   This constant bit is compatible with Microsoft and retains
UUID. Reserved_future
   This constant is reserved for future possible definitions
You can view these constants in Python:
>>> UUID. Reserved_ncs ' RESERVED for NCS compatibility ' >>> uuid. rfc_4122 ' specified in RFC 4122 ' >>> uuid. Reserved_microsoft ' RESERVED for MICROSOFT compatibility ' >>> uuid. Reserved_future ' RESERVED for future definition '

Python--uuid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.