用python在linux下获取网卡地址

时间:2021-09-09 16:14:37

使用一个第三方库,名为psutil

代码如下:

#!/usr/bin/python
# -*- coding:utf-8 -*- import psutil def get_netcard():
  netcard_info = []
  info = psutil.net_if_addrs()
  for k,v in info.items():
    # print "k is {0} and v is {1}".format(k,v)
    for item in v:
      if item[0] == 2 and not item[1] == '127.0.0.1':
        netcard_info.append(item[1])
  return netcard_info if __name__ == '__main__':
  print get_netcard()