使用一個第三方庫,名爲psutilpython
代碼以下:app
#!/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()