如何在Python中创建GUID / UUID

How to create a GUID/UUID in Python

如何在python中创建一个独立于平台的guid?我听说有一种方法在Windows上使用ActivePython,但它是Windows,因为它使用COM。有没有使用纯Python的方法?


The uuid module, in Python 2.5 and up, provides RFC compliant UUID
generation. See the module docs and the RFC for details.

文档:P></

  • Python的:http://2 / 2 /图书馆/ uuid.html docs.python.org
  • 3:Pythondocs.python.org HTTPS:/ / / / / 3 uuid.html图书馆

example(2和3)工作):P></

1
2
3
4
5
6
7
>>> import uuid
>>> uuid.uuid4()
UUID('bd65600d-8669-4903-8a14-af88203add38')
>>> str(uuid.uuid4())
'f50ec0b7-f960-400d-91f0-c42a6d44e3d0'
>>> uuid.uuid4().hex
'9fe2c4e93f654fdbb24c02b15259716c'


如果你使用Python 2.5或UUID is already模以后,the included with the Python标准分布。P></

前:P></

1
2
3
>>> import uuid
>>> uuid.uuid4()
UUID('5361a11b-615c-42bf-9bdb-e2c3790ada14')


copied from:HTTPS:/ / / 2 /图书馆/ docs.python.org uuid.html(since the links posted是not主动和他们继续教育)P></

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
26
27
28
29
30
31
32
33
34
>>> 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
\x0b\x0c
\x0e\x0f'


>>> # make a UUID from a 16-byte string
>>> uuid.UUID(bytes=x.bytes)
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')

使用随机密钥的guid as for型数据库操作。P></

the hexadecimal form with the characters,dashes和额外的unnecessarily似乎对我的长。但我也喜欢那串数字是representing a hexadecimal that they do not甚安全问题的原因,可以在characters contain some such as情况+,=,等等。P></

hexadecimal instead of a,安全使用base64字符串的URL。conform to the following does not any UUID的GUID(规格/虽然比其他有randomness amount of the required)。P></

1
2
3
4
5
6
7
import base64
import uuid

# get a UUID - URL safe, Base64
def get_a_uuid():
    r_uuid = base64.urlsafe_b64encode(uuid.uuid4().bytes)
    return r_uuid.replace('=', '')


这个函数和UID是独特的基于全configurable generates on the specified formatP></

EG:[ 4,8,4,4,12),this is the format the following和EN将上述生成的UUIDP></

LxoYNyXe-7hbQ-caJt-DSdU-PDAht56cMEWi

1
2
3
4
5
6
7
8
9
10
11
12
 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