Python: Get information such as native IP gateways using the Netifaces module

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.