日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2025/3/21 python 58 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python获取机器唯一标识_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

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/

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的python获取机器唯一标识_python中uuid来生成机器唯一标识的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。