Windows getting local ip address

Discussion in 'Python' started by SolaFide, Mar 22, 2006.

  1. SolaFide

    SolaFide Guest

    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?
     
    SolaFide, Mar 22, 2006
    #1
    1. Advertising

  2. SolaFide

    utabintarbo Guest

    You can do essentially the same thing substituting "ipconfig" for
    ifconfig.

    Though I am sure there are better ways....
     
    utabintarbo, Mar 22, 2006
    #2
    1. Advertising

  3. "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>
     
    Fredrik Lundh, Mar 22, 2006
    #3
  4. SolaFide

    Arne Ludwig Guest

    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'
     
    Arne Ludwig, Mar 22, 2006
    #4
  5. SolaFide

    Erno Kuusela Guest

    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
     
    Erno Kuusela, Mar 23, 2006
    #5
  6. SolaFide

    Arne Ludwig Guest

    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', 2768)
    >>> s.close()


    Should work on Windows as well.
     
    Arne Ludwig, Mar 23, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. avnrao
    Replies:
    1
    Views:
    7,231
    avnrao
    May 4, 2004
  2. Patrice
    Replies:
    0
    Views:
    890
    Patrice
    May 4, 2004
  3. Raterus
    Replies:
    0
    Views:
    438
    Raterus
    Jun 2, 2004
  4. Booter

    Getting Local MAC Address

    Booter, Apr 2, 2010, in forum: Python
    Replies:
    12
    Views:
    1,686
    Rebelo
    Apr 7, 2010
  5. moijes12
    Replies:
    1
    Views:
    309
    moijes12
    May 22, 2010
Loading...

Share This Page