python获取mac地址的方法

方法一:

  借助uuid模块

  def get_mac_address():
  import uuid
      node = uuid.getnode()
      mac = uuid.UUID(int = node).hex[-12:]
  return mac

方法二:

  按操作系统平台来:

  def get_mac_address():
    '''
    @summary: return the MAC address of the computer
    '''
    import sys
    import os
    mac = None
    if sys.platform == "win32":
        for line in os.popen("ipconfig /all"):
            print line
            if line.lstrip().startswith("Physical Address"):
                mac = line.split(":")[1].strip().replace("-", ":")
                break
    else:
        for line in os.popen("/sbin/ifconfig"):
            if 'Ether' in line:
                mac = line.split()[4]
                break
    return mac

个人推荐方法一,简单通用

猜你喜欢

转载自www.cnblogs.com/jubing/p/10968592.html