笔者在项目中遇到过获取本机网卡ip的例子,利用python库psutil解决了此问题。
def get_netcard(): """获取网卡名称和ip地址 """ netcard_info = [] info = psutil.net_if_addrs() for k, v in info.items(): for item in v: if item[0] == 2 and not item[1] == '127.0.0.1': netcard_info.append((k, item[1])) return netcard_info if __name__ == '__main__': print get_netcard()