2 回答

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

TA貢獻1111條經驗 獲得超0個贊
首先聲明,我本人還沒有研究出來問題的究竟。此處只是寫下我本人的一點小心得,大家一起進步。
因為我發現,使用uuid庫得到的mac地址,總有最后一位不對。所以,我就查看了python官方的uuid文檔,找到了問題的關鍵是調用UUID()的時候,會調用getnode()函數以得到物理地址。
這個是getnode()函數的定義:
我把它摘出來,考到下面。
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調用系統庫,從而執行系統自帶的命令:(類似于windows下cmd里面"ipconfig -all"命令,或者ubuntu下terminal中"ifconfig"命令)。實現物理地址。之后,根據“短時間內該機器的網卡不會出現過大的變動這個前提”,我們可以根據返回內容,切片出我們需要的部分即可。
添加回答
舉報