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/
python中uuid来生成机器唯一标识的更多相关文章
- 使用UUID方法生成全球唯一标识
需要生成唯一字符串,如生成应用标识等,可以直接用java.util.UUID类实现. UUID(Universally Unique Identifier)全局唯一标识符,是指在一台机器上生成的数字, ...
- Python中random模块生成随机数详解
Python中random模块生成随机数详解 本文给大家汇总了一下在Python中random模块中最常用的生成随机数的方法,有需要的小伙伴可以参考下 Python中的random模块用于生成随机数. ...
- python使用uuid库生成唯一id
概述: UUID是128位的全局唯一标识符,通常由32字节的字符串表示. 它可以保证时间和空间的唯一性,也称为GUID,全称为: UUID -- Universally Unique IDentifi ...
- Python使用UUID库生成唯一ID(转)
原文:http://www.cnblogs.com/dkblog/archive/2011/10/10/2205200.html 资料: Python官方Doc:<20.15. uuid — U ...
- [py]Python使用UUID库生成唯一ID(uuid模块)
https://www.cnblogs.com/dkblog/archive/2011/10/10/2205200.html uuid介绍 UUID是128位的全局唯一标识符,通常由32字节的字符串表 ...
- IOS 生成设备唯一标识
前言 iOS设备5.0以上放弃使用[[UIDevice currentDevice] uniqueIdentifier]来获得设备唯一ID iOS设备私有方法禁止用户获取和使用IMEI 需求 需要一个 ...
- Python中随机数的生成
在Python中要实现随机数的生成,需要使用random模块中randint方法. 其具体实现方法如下: import random a = random.randint(1,20) #(1,20)为 ...
- 生成全球唯一标识GUID
有时候我们操作数据的时候需要给这些数据一些编码,而这些编码又希望永远不会重复!这个时候微软的C#给了我们一个函数,这个函数产生的编码全球唯一,永远不会重复! 方法如下: 1.C#生成方式 string ...
- python 使用UUID库生成唯一ID
首先导包: import uuid uuid1(): # make a UUID based on the host ID and current time # 基于MAC地址,时间 ...
随机推荐
- SQL函数学习(二):DATEADD() 函数
DATEADD() 函数在日期中添加或减去指定的时间间隔. 语法 DATEADD(datepart,number,date) date 参数是合法的日期表达式.number 是您希望添加的间隔数:对于 ...
- knockout.js-创建视图模型
监控属性(Observables) knockout的三个核心特点: 1.监控属性与依赖跟踪 2.声明式绑定 3.模板 本页,你将学习上述三个特性.但是在这之前,先了解一下MVVC模式,及 视图模型( ...
- Oracle Day07 PL/SQL基础
1.基本格式 set serveroutput on declare -- 申明部分 name ); begin -- 执行的sql语句 ; dbms_output.put_line(name); e ...
- 1.Perl 多线程:Threads
详情可查看: perldoc threads 调用线程的方法: $thr = threads->create(FUNCTION, ARGS) #This will create a new th ...
- 使用非 GUI 模式运行 JMeter 压力测试
使用非 GUI 模式,即命令行模式运行 JMeter 测试脚本能够大大缩减所需要的系统资源.使用命令jmeter -n -t <testplan filename> -l <list ...
- Python第三方库安装技巧
pytho下有三种安装第三方库方法: 1.通过easy_install安装 2.通过Pip安装 前面两种,由于受国内部门网站原因,如果安装失败,可采用接下来的第三种方法 3.在指定网站下载安装 第三方 ...
- Linux设置开机服务自动启动
[root@localhost ~]# chkconfig --list 显示开机可以自动启动的服务[root@localhost ~]# chkconfig --add *** 添加开机自 ...
- Linux Shell 小脚本经典收藏
原文:http://www.cnblogs.com/Javame/p/3867686.html 1.在两个文件中找出相同的号码 diff -y xx.txt oo.txt | egrep -v &qu ...
- html/css技巧总结
.e-select .on{display:none} on为e-select的子元素,(之间有空格).e-select.on{display:block} 只有两种属性同时存在时才会起作用(之间无空 ...
- log4j.xml(信息打印)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SY ...