Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Windows getting local ip address

Thread Tools

Windows getting local ip address

Posts: n/a
On Linux, it is a simple matter to get the local ip address with
system.os("ifconfig >> /tmp/ip"); ip=open("/tmp/ip").readlines(), etc.
How can I do this with Windows?

Reply With Quote
Posts: n/a
You can do essentially the same thing substituting "ipconfig" for

Though I am sure there are better ways....

Reply With Quote
Fredrik Lundh
Posts: n/a
"SolaFide" wrote:

> On Linux, it is a simple matter to get the local ip address with
> system.os("ifconfig >> /tmp/ip"); ip=open("/tmp/ip").readlines(), etc.

ip = os.popen("ifconfig").readlines()

is a bit more convenient.

> How can I do this with Windows?

the command is called "ipconfig" in windows.

there's also

>>> import socket
>>> socket.gethostbyname(socket.gethostname())

>>> socket.gethostbyname_ex(socket.gethostname())

('', ['bender'], [''])
>>> socket.getaddrinfo(socket.gethostname(), 0)

[(2, 1, 0, '', ('', 0)), (2, 2, 0, '', ('', 0))]

etc. if you're behind a firewall/NAT etc and you want your "public IP",
you can do something like:

>>> import re, urllib
>>> ip = urllib.urlopen('').read()
>>>"(\d+\.\d+\.\d+.\d+)", ip).group()


hope this helps!


Reply With Quote
Arne Ludwig
Posts: n/a
The second solution can give really weird results though, e.g. on my
Linux system I get:

>>> gethostbyaddr(gethostname())

('', ['linux'], [''])

A more flexible but potentially unportable way would be:

>>> import socket
>>> import fcntl
>>> import struct
>>> 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])
>>> get_ip_address('eth0')


Reply With Quote
Erno Kuusela
Posts: n/a
The traditional right way (tm) to do this is to call getsockname() on
the (a?) socket that's connected to the guy you want to tell your
address to. This picks the right address in case you have several. If
you don't have a socket handy, you can make a connectionless UDP
socket and connect() it to a suitable place - this won't result in any
packets on the wire. NAT breaks it of course, but then you couldn't
easily be contacted from outside the NAT anyway.

-- erno
Reply With Quote
Arne Ludwig
Posts: n/a
That man is a genius:

>>> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>>> s.connect(("",80))
>>> print s.getsockname()

('', 276
>>> s.close()

Should work on Windows as well.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
getting local area connection information through python in windows moijes12 Python 1 05-22-2010 08:17 AM
Getting Local MAC Address Booter Python 12 04-07-2010 07:32 AM
.NET 2.0: Sending email on local domain to local exchange 2K server Jim in Arizona ASP .Net 8 01-24-2006 05:37 PM
Access local port or Running local exe file =?Utf-8?B?WVNVVA==?= ASP .Net 0 01-14-2006 12:41 AM
Can't use 'local' to find sql server instances on local machine karim ASP .Net 1 06-26-2003 09:17 PM