Path: csiph.com!x330-a1.tempe.blueboxinc.net!aioe.org!feeder.news-service.com!news2.euro.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'received:verizon.net': 0.07; 'python': 0.07; 'parsing': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'wifi': 0.09; 'linux': 0.11; 'interfaces': 0.12; 'output': 0.12; 'def': 0.13; 'wrote:': 0.14; '16)': 0.16; 'os:': 0.16; 'received:east.verizon.net': 0.16; 'subject:interface': 0.16; 'subject:address': 0.16; 'shell': 0.19; 'command': 0.19; 'interface': 0.20; 'script': 0.26; 'windows': 0.26; 'to:addr:python-list': 0.32; 'header:X-Complaints-To:1': 0.34; 'received:70': 0.34; 'there': 0.35; 'question': 0.35; 'header:User-Agent:1': 0.35; 'none': 0.36; 'two': 0.37; 'some': 0.37; 'skip:o 20': 0.37; 'connected': 0.37; 'useful': 0.37; 'received:org': 0.38; 'to:addr:python.org': 0.39; 'header:Mime- Version:1': 0.39; 'how': 0.39; 'header:Received:5': 0.40; 'address': 0.61; 'subject:Get': 0.74 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Neal Becker Subject: Re: Get the IP address of WIFI interface Followup-To: gmane.comp.python.general Date: Sun, 15 May 2011 07:04:27 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: pool-70-109-68-60.hag.east.verizon.net User-Agent: KNode/4.4.11 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 32 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305457484 news.xs4all.nl 34849 [::ffff:82.94.164.166]:56915 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5420 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])