2 回答

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超8個贊
>>> import socket
>>> myname = socket.getfqdn(socket.gethostname())
>>> socket.gethostbyname(myname)
通過此方法獲得的IP看看,是不是也與實(shí)際對應(yīng)不上?

TA貢獻(xiàn)1111條經(jīng)驗(yàn) 獲得超0個贊
首先聲明,我本人還沒有研究出來問題的究竟。此處只是寫下我本人的一點(diǎn)小心得,大家一起進(jìn)步。
因?yàn)槲野l(fā)現(xiàn),使用uuid庫得到的mac地址,總有最后一位不對。所以,我就查看了python官方的uuid文檔,找到了問題的關(guān)鍵是調(diào)用UUID()的時候,會調(diào)用getnode()函數(shù)以得到物理地址。
這個是getnode()函數(shù)的定義:
我把它摘出來,考到下面。
def getnode(*, getters=None):
"""Get the hardware address as a 48-bit positive integer.
The first time this runs, it may launch a separate program, which could
be quite slow. If all attempts to obtain the hardware address fail, we
choose a random 48-bit number with its eighth bit set to 1 as recommended
in RFC 4122.
"""
global _node
if _node is not None:
return _node
if sys.platform == 'win32':
getters = _NODE_GETTERS_WIN32
else:
getters = _NODE_GETTERS_UNIX
for getter in getters + [_random_getnode]:
try:
_node = getter()
except:
continue
if (_node is not None) and (0 <= _node < (1 << 48)):
return _node
assert False, '_random_getnode() returned invalid value: {}'.format(_node)
我正在試圖通過研究這個問題來試圖研究。但同樣,我覺得我們可以直接讓python調(diào)用系統(tǒng)庫,從而執(zhí)行系統(tǒng)自帶的命令:(類似于windows下cmd里面"ipconfig -all"命令,或者ubuntu下terminal中"ifconfig"命令)。實(shí)現(xiàn)物理地址。之后,根據(jù)“短時間內(nèi)該機(jī)器的網(wǎng)卡不會出現(xiàn)過大的變動這個前提”,我們可以根據(jù)返回內(nèi)容,切片出我們需要的部分即可。
添加回答
舉報(bào)