python中uuid来生成机器唯一标识

来源:互联网 发布:网络推广的工作内容 编辑:程序博客网 时间:2024/05/16 12:30

python中uuid来生成机器唯一标识


摘要:

我们可以使用uuid1的后16位来标识一个机器。

 # use machine specific uuid, last 16 char will be the same if machine is the same
 mid = uuid.uuid1().get_hex()[16:]




1  uuid的其他模块  概述:

    UUID是128位的全局唯一标识符,通常由32字节的字符串表示。
    它可以保证时间和空间的唯一性,也称为GUID,全称为:
            UUID —— Universally Unique IDentifier Python 中叫 UUID
            GUID —— Globally Unique IDentifier C# 中叫 GUID

    它通过MAC地址、时间戳、命名空间、随机数、伪随机数来保证生成ID的唯一性。
    UUID主要有五个算法,也就是五种方法来实现:

       1、uuid1()——基于时间戳
        
               由MAC地址、当前时间戳、随机数生成。可以保证全球范围内的唯一性,
               但MAC的使用同时带来安全性问题,局域网中可以使用IP来代替MAC。

       2、uuid2()——基于分布式计算环境DCE(Python中没有这个函数)

                算法与uuid1相同,不同的是把时间戳的前4位置换为POSIX的UID。
                实际中很少用到该方法。

      3、uuid3()——基于名字的MD5散列值
             
                通过计算名字和命名空间的MD5散列值得到,保证了同一命名空间中不同名字的唯一性,
                和不同命名空间的唯一性,但同一命名空间的同一名字生成相同的uuid。
 
       4、uuid4()——基于随机数

                由伪随机数得到,有一定的重复概率,该概率可以计算出来。

       5、uuid5()——基于名字的SHA-1散列值

                算法与uuid3相同,不同的是使用 Secure Hash Algorithm 1 算法

使用方面:
    
    首先,Python中没有基于DCE的,所以uuid2可以忽略;
    其次,uuid4存在概率性重复,由无映射性,最好不用;
    再次,若在Global的分布式计算环境下,最好用uuid1;
    最后,若有名字的唯一性要求,最好用uuid3或uuid5。

编码方法:

    # -*- coding: utf-8 -*-
    
    import uuid

    name = "test_name"
    namespace = "test_namespace"

    print uuid.uuid1() # 带参的方法参见Python Doc
    print uuid.uuid3(namespace, name)
    print uuid.uuid4()
    print uuid.uuid5(namespace, name)



2  uuid1

对照下边代码我们可以看到uuid1的构成。

代码:

import uuid
u = uuid.uuid1()


print u
print type(u)
print 'bytes   :', repr(u.bytes)
print 'hex     :', u.hex
print 'int     :', u.int
print 'urn     :', u.urn
print 'variant :', u.variant
print 'version :', u.version
print 'fields  :', u.fields
print '\ttime_low            : ', u.time_low
print '\ttime_mid            : ', u.time_mid
print '\ttime_hi_version     : ', u.time_hi_version
print '\tclock_seq_hi_variant: ', u.clock_seq_hi_variant
print '\tclock_seq_low       : ', u.clock_seq_low
print '\tnode                : ', u.node
print '\ttime                : ', u.time
print '\tclock_seq           : ', u.clock_seq
print '\ttime_low            : ', hex(u.time_low)
print '\ttime_mid            : ', hex(u.time_mid)
print '\ttime_hi_version     : ', hex(u.time_hi_version)
print '\tclock_seq_hi_variant: ', hex(u.clock_seq_hi_variant)
print '\tclock_seq_low       : ', hex(u.clock_seq_low)
print '\tnode                : ', hex(u.node)
print '\ttime                : ', hex(u.time)
print '\tclock_seq           : ', hex(u.clock_seq)


结果:

f38f7a10-2e83-11e4-9073-90b11c00c5b4
<class 'uuid.UUID'>
bytes   : '\xf3\x8fz\x10.\x83\x11\xe4\x90s\x90\xb1\x1c\x00\xc5\xb4'
hex     : f38f7a102e8311e4907390b11c00c5b4
int     : 323747377162522047429174169111671915956
urn     : urn:uuid:f38f7a10-2e83-11e4-9073-90b11c00c5b4
variant : specified in RFC 4122
version : 1
fields  : (4086266384L, 11907L, 4580L, 144L, 115L, 159090353423796L)
time_low            :  4086266384
time_mid            :  11907
time_hi_version     :  4580
clock_seq_hi_variant:  144
clock_seq_low       :  115
node                :  159090353423796
time                :  136285032989817360
clock_seq           :  4211
time_low            :  0xf38f7a10L
time_mid            :  0x2e83L
time_hi_version     :  0x11e4L
clock_seq_hi_variant:  0x90L
clock_seq_low       :  0x73L
node                :  0x90b11c00c5b4L
time                :  0x1e42e83f38f7a10L
clock_seq           :  0x1073L




参考资料:(1)python  uuid 模块介绍 http://pymotw.com/2/uuid/

                      (2)rfc4122文档对uuid的规定     http://www.rfc-editor.org/rfc/rfc4122.txt

                       (3)某人的说明  http://www.dongwm.com/archives/guanyuuuidyanjiu/

0 0
原创粉丝点击