Python gets native IP in a number of ways, each of which has its limitations.
Using the Netifaces module to obtain information such as a native IP gateway, you need to install the Netifaces module, whether Windows or Linux can be common.
First, the procedure:
#!/usr/bin/env python2# -*- coding: utf-8 -*-#实现本地网卡IP # need to install Netifaces module def Getnetworkip (): #获取本地网卡IP地址 import netifaces #routingGateway = netifaces.gateways () [' Default '][netifaces.af_inet][0] #网关 routingnicname = netifaces.gateways () [' Default '][netifaces.af_inet] [1] #网络适配器信息 for interface in netifaces.interfaces (): if interface == routingNicName: #print (netifaces.ifaddresses (interface)) try: routingIPAddr = Netifaces.ifaddresses (interface) [netifaces.af_inet][0]['Addr '] #获取IP except KeyError: pass #print ("routing ip address:%s"% routingipaddr) return routingIPAddrif __name__ == "__main__": try: print ("routing ip address:", GetNetworkIP () ) except: print ("Unable to get the address, there may not be installed netifaces Module! command: pip install netifaces ")
Two:
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/8A/9E/wKiom1g1R1iBMtCQAAA8X5C5VlI974.png-wh_500x0-wm_3 -wmp_4-s_918982743.png "title=" 2.png "alt=" Wkiom1g1r1ibmtcqaaa8x5c5vli974.png-wh_50 "/>
This article is from the "Urban Cloth" blog, please be sure to keep this source http://sunday208.blog.51cto.com/377871/1875822
Python: Get information such as native IP gateways using the Netifaces module