Python:获取MAC地址

目录

方法一

方法二


方法一

import uuid

# 获取本机MAC地址
def get_local_mac():
    mac_address = ':'.join(hex(uuid.getnode())[2:].zfill(12)[i:i + 2] for i in range(0, 12, 2))
    print("MAC address:", mac_address)

if __name__=='__main__':
    get_local_mac()

运行结果:

D:\Python3.8.6\python.exe D:/PythonWorkSpace/someip/Common/get_mac_address.py
MAC address: 00:50:56:c0:00:08

Process finished with exit code 0

方法二

from psutil import net_if_addrs

# 获取所有MAC地址
def get_all_mac():
    for k, v in net_if_addrs().items():
        for item in v:
            address = item[1]
            if '-' in address and len(address)==17:
                print(address)

if __name__=='__main__':
    get_all_mac()

运行结果:

D:\Python3.8.6\python.exe D:/PythonWorkSpace/someip/Common/get_mac_address.py
94-C6-91-D2-FC-B5
00-50-56-C0-00-01
00-50-56-C0-00-08
02-00-00-0B-00-03
00-FF-AA-24-85-5D

Process finished with exit code 0

你可能感兴趣的:(python,开发语言)