摘要:

我们可以使用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/

python中uuid来生成机器唯一标识的更多相关文章

  1. 使用UUID方法生成全球唯一标识

    需要生成唯一字符串,如生成应用标识等,可以直接用java.util.UUID类实现. UUID(Universally Unique Identifier)全局唯一标识符,是指在一台机器上生成的数字, ...

  2. Python中random模块生成随机数详解

    Python中random模块生成随机数详解 本文给大家汇总了一下在Python中random模块中最常用的生成随机数的方法,有需要的小伙伴可以参考下 Python中的random模块用于生成随机数. ...

  3. python使用uuid库生成唯一id

    概述: UUID是128位的全局唯一标识符,通常由32字节的字符串表示. 它可以保证时间和空间的唯一性,也称为GUID,全称为: UUID -- Universally Unique IDentifi ...

  4. Python使用UUID库生成唯一ID(转)

    原文:http://www.cnblogs.com/dkblog/archive/2011/10/10/2205200.html 资料: Python官方Doc:<20.15. uuid — U ...

  5. [py]Python使用UUID库生成唯一ID(uuid模块)

    https://www.cnblogs.com/dkblog/archive/2011/10/10/2205200.html uuid介绍 UUID是128位的全局唯一标识符,通常由32字节的字符串表 ...

  6. IOS 生成设备唯一标识

    前言 iOS设备5.0以上放弃使用[[UIDevice currentDevice] uniqueIdentifier]来获得设备唯一ID iOS设备私有方法禁止用户获取和使用IMEI 需求 需要一个 ...

  7. Python中随机数的生成

    在Python中要实现随机数的生成,需要使用random模块中randint方法. 其具体实现方法如下: import random a = random.randint(1,20) #(1,20)为 ...

  8. 生成全球唯一标识GUID

    有时候我们操作数据的时候需要给这些数据一些编码,而这些编码又希望永远不会重复!这个时候微软的C#给了我们一个函数,这个函数产生的编码全球唯一,永远不会重复! 方法如下: 1.C#生成方式 string ...

  9. python 使用UUID库生成唯一ID

      首先导包: import uuid   uuid1(): # make a UUID based on the host ID and current time     #  基于MAC地址,时间 ...

随机推荐

  1. eclipse和tomcat整合之后每次发布server.xml被修改(转)

    eclipse每次发布,server.xml和context.xml总是被还原 直接找到eclispse工程下的server工程,把里面的相应的server.xml和context.xml修改了即可, ...

  2. sql注入绕过union select过滤

    # # # #WAF Bypassing Strings: /*!%55NiOn*/ /*!%53eLEct*/ ,,)-- - +union+distinct+select+ +union+dist ...

  3. 3.Perl 多线程:Threads(exit thread_only)

    还可以在导入threads模块时设置: use threads ('exit' => 'thread_only');

  4. php 关了浏览器也可以自动运行脚本

    <?php ignore_user_abort(); //即使Client断开(如关掉浏览器),PHP脚本也可以继续执行. set_time_limit(0); //执行时间为无限制,php默认 ...

  5. 缺少对象 WScript 问题解决方法

    方法一: 先把脚本保存起来(保证你的脚本能正确运行),例如命名为test.vbs 然后在QTP写以下脚本就可以正确运行了! Dim oShell Set oShell =CreateObject (& ...

  6. json格式数据,将数据库中查询的结果转换为json, 然后调用接口的方式返回json(方式一)

    调用接口,无非也就是打开链接 读取流 将结果以流的形式输出 将查询结果以json返回,无非就是将查询到的结果转换成jsonObject ================================ ...

  7. LeetCode OJ 292.Nim Game

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  8. find the greatest common divisor

    function gcd(a, b) return a else return gcd(b, a mod b)

  9. AIR使用文件对象操作文件和目录

    文件对象是啥?文件对象(File对象)是在文件系统中指向文件或目录的指针.由于安全原因,只在AIR中可用. 文件对象能做啥? 获取特定目录,包括用户目录.用户文档目录.该应用程序启动的目录和程序目录 ...

  10. ios 导航页面

    //  AppDelegate.m#import "AppDelegate.h"#import "ViewController.h" @interface Ap ...