python 使用uuid设置唯一值
来源:互联网 发布:软件研发部助理 编辑:程序博客网 时间:2024/05/01 23:14
最近需要用到一个唯一值,想起了UUID,python2.4.3没有自带这个模块,需要单独安装:
1.下载:去pypi下载tar包
http://pypi.python.org/pypi/uuid/
直接下载:
2.安装
tar xzvf uuid-1.30.tar.gz
cd uuid-1.30
sudo python setup.py install
3.功能说明:
它通过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 算法
4.用法
>>> uuid.uuid1()UUID('c386952e-86b0-11e1-9b99-0050569b2af7')>>> uuid.uuid4()UUID('6d808f11-99cc-49b2-97b3-1d6985d1af3f')
python下面uuid的使用
最近经常用到,总结一下。import uuid
>>> print uuid.uuid1()5cfdedb0-01e8-11df-ba6a-001636e99c1a
>>> uuid.uuid1().get_hex()'7b6d5f0f01e811dfbf6d001636e99c1a'
# 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\n\x0b\x0c\r\x0e\x0f' # make a UUID from a 16-byte string >>> uuid.UUID(bytes=x.bytes) UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')
>>> dir(uuid)['NAMESPACE_DNS', 'NAMESPACE_OID', 'NAMESPACE_URL', 'NAMESPACE_X500', 'RESERVED_FUTURE', 'RESERVED_MICROSOFT', 'RESERVED_NCS', 'RFC_4122', 'UUID', '_UuidCreate', '__author__', '__builtins__', '__doc__', '__file__', '__name__', '_buffer', '_find_mac', '_ifconfig_getnode', '_ipconfig_getnode', '_last_timestamp', '_netbios_getnode', '_node', '_random_getnode', '_unixdll_getnode', '_uuid_generate_random', '_uuid_generate_time', '_windll_getnode', 'ctypes', 'getnode', 'lib', 'libname', 'uuid1', 'uuid3', 'uuid4', 'uuid5']
>>> dir(uuid.uuid1())['__class__', '__cmp__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__int__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', 'bytes', 'bytes_le', 'clock_seq', 'clock_seq_hi_variant', 'clock_seq_low', 'fields', 'get_bytes', 'get_bytes_le', 'get_clock_seq', 'get_clock_seq_hi_variant', 'get_clock_seq_low', 'get_fields', 'get_hex', 'get_node', 'get_time', 'get_time_hi_version', 'get_time_low', 'get_time_mid', 'get_urn', 'get_variant', 'get_version', 'hex', 'int', 'node', 'time', 'time_hi_version', 'time_low', 'time_mid', 'urn', 'variant', 'version']
- python 使用uuid设置唯一值
- Python使用UUID库生成唯一ID
- Python使用UUID库生成唯一ID
- Python使用UUID库生成唯一ID
- Python使用UUID生成唯一ID
- Python使用UUID库生成唯一ID
- Python使用UUID库生成唯一ID
- Python使用UUID库生成唯一ID
- Python使用UUID库生成唯一ID
- Python中生成唯一码(uuid的使用)
- python 中使用UUID库生产唯一ID
- python使用uuid生成唯一id或str
- python uuid生成唯一索引
- 全局唯一ID--UUID介绍、JAVA中UUID的使用
- 使用UUID生成全球唯一标识
- iOS 中UUID唯一标记的使用
- YII2.0 使用UUID 生成唯一字符串
- UUID介绍, Python使用UUID库
- Label是否识别\n?
- Python转义字符
- history
- 编译原理——四种文法类型
- Navigation Bar开机后显示和隐藏 (一)
- python 使用uuid设置唯一值
- 一个值得Geek收藏的网站
- pku1001
- 内存_命令行编译_运行_多个类day02
- UIView 圆角
- 冷伤风,热感冒!同是感冒药,治的感冒仍有不同。
- 获取MAC地址
- 推荐C++博客专栏
- python 临时文件夹 的 tempfile模块学习