Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5420

Re: Get the IP address of WIFI interface

From Neal Becker <ndbecker2@gmail.com>
Subject Re: Get the IP address of WIFI interface
Followup-To gmane.comp.python.general
Date 2011-05-15 07:04 -0400
References <BANLkTi=YN7CW_y3w5EK-36uPwPcxXgPm8A@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1591.1305457483.9059.python-list@python.org> (permalink)

Followups directed to: gmane.comp.python.general

Show all headers | View raw


Far.Runner wrote:

> Hi python experts:
> There are two network interfaces on my laptop: one is 100M Ethernet
> interface, the other is wifi interface, both are connected and has an ip
> address.
> The question is: How to get the ip address of the wifi interface in a python
> script without parsing the output of a shell command like "ipconfig" or
> "ifconfig"?
> 
> OS: Windows or Linux
> 
> F.R

Here's some useful snippits for linux:

def get_default_if():
    f = open('/proc/net/route')
    for i in csv.DictReader(f, delimiter="\t"):
        if long(i['Destination'], 16) == 0:
            return i['Iface']
    return None

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: Get the IP address of WIFI interface Neal Becker <ndbecker2@gmail.com> - 2011-05-15 07:04 -0400
  Re: Get the IP address of WIFI interface Anssi Saari <as@sci.fi> - 2011-05-16 22:57 +0300

csiph-web