python脚本netifaces模块的调用

# vim get_ip.py

# -*- coding: utf- -*-
#complete local network card IP
#need install netifaces modem
def GetNetworkIP():
#get local network card ip address
import netifaces
routingGateway = netifaces.gateways()['default'][netifaces.AF_INET][] #gateway
routingNicName = netifaces.gateways()['default'][netifaces.AF_INET][] #NA(network adapter)information for interface in netifaces.interfaces():
if interface == routingNicName:
#print (netifaces.ifaddresses(interface))
try:
routingIPAddr = netifaces.ifaddresses(interface)[netifaces.AF_INET][]['addr'] #get IP
except KeyError:
pass
#print ("Routing IP Address:%s"% routingIPAddr)
return routingIPAddr
if __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")

#python get_ip.py

('Routing IP Address:', '192.xx.xx.xx')

参考文献:http://blog.51cto.com/sunday208/1875822

上一篇:ARM汇编编程基础之一 —— 寄存器


下一篇:xhtml和html的区别 html5和xhtml的区别