在Python中获取MAC地址的方法如下:
1. 使用 `uuid` 模块:
```python
import uuid
def get_mac_address():
node = uuid.getnode()
mac = uuid.UUID(int=node).hex[-12:]
return ':'.join([mac[i:i+2] for i in range(0, 11, 2)])
2. 根据操作系统平台:
对于Windows系统:
```python
import os
def get_mac_address_windows():
for line in os.popen('ipconfig /all'):
if line.lstrip().startswith('Physical Address'):
return line.split(':').strip().replace('-', '')
对于Linux系统:
```python
import os
def get_mac_address_linux():
for line in os.popen('/sbin/ifconfig'):
if 'Ether' in line:
return line.split()
3. 使用 `wmi` 库(仅适用于Windows系统):
```python
import wmi
def get_mac_address_wmi():
c = wmi.WMI()
network = c.Win32_NetworkAdapterConfiguration(IPEnabled=1)
for nw in network:
return nw.MACAddress
请根据您的操作系统选择合适的方法。