当缺少UUID模块时,如何在python2.4中生成UUID/GUID

2024-09-28 21:22:40 发布

您现在位置:Python中文网/ 问答频道 /正文

无法在Python2.4中找到生成UUID的方法,因为该模块在Python2.5中可用,在2.7中稳定。我的机器是centos5&由于其他依赖项不能真正使用或升级到python2.7


Tags: 模块方法机器uuidcentos5
3条回答

我用的是

#!/usr/bin/env python

import commands

def uuid():
        return commands.getstatusoutput('uuidgen')

if __name__ == "__main__":
        print uuid()[1]

它给出了很好的预期答案e79a890c-5e3a-4c3a-bfdb-5377389b69ac

Linux内核支持生成UUID。在我的Debian系统上,可以从/proc/sys/kernel/random/uuid处的/proc伪文件系统访问此功能:

>>> with open('/proc/sys/kernel/random/uuid') as f:
...     print(f.read())
... 
1e21ee4f-953e-4179-9bea-ac9a0b9189e7

如果您能够安装libuuid,您将可以访问uuidgen命令行工具:

From man uuidgen

The uuidgen program creates (and prints) a new universally unique identifier (UUID) using the libuuid(3) library. The new UUID can reasonably be considered unique among all UUIDs created on the local system, and among UUIDs created on other systems in the past and in the future.

^{pr2}$

使用这个:https://gist.github.com/mahmoudimus/56bcec09b69a2b5165aa

它是uuid模块的python2.3+兼容版本。在

示例:

curl -O https://gist.githubusercontent.com/mahmoudimus/56bcec09b69a2b5165aa/raw/b1dd7633fff6ca0cc84a3b1fe435db7c65180dac/uuid.py

使用:

^{pr2}$

也可以使用:https://pypi.python.org/pypi/pyuuid/0.0.1

pip install pyuuid

相关问题 更多 >