Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Windows getting local ip address

 
 
SolaFide
Guest
Posts: n/a
 
      03-22-2006
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
 
 
 
 
utabintarbo
Guest
Posts: n/a
 
      03-22-2006
You can do essentially the same thing substituting "ipconfig" for
ifconfig.

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

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      03-22-2006
"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())

'1.2.3.4'
>>> socket.gethostbyname_ex(socket.gethostname())

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

[(2, 1, 0, '', ('1.2.3.4', 0)), (2, 2, 0, '', ('1.2.3.4', 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('http://checkip.dyndns.org').read()
>>> re.search("(\d+\.\d+\.\d+.\d+)", ip).group()

'4.3.2.1'

hope this helps!

</F>



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

>>> gethostbyaddr(gethostname())

('linux.site', ['linux'], ['127.0.0.2'])

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')

'192.168.0.174'

 
Reply With Quote
 
Erno Kuusela
Guest
Posts: n/a
 
      03-23-2006
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
Guest
Posts: n/a
 
      03-23-2006
That man is a genius:

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

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


Should work on Windows as well.

 
Reply With Quote
 
 
 
Reply

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



Advertisments