Re: Finding IP address of localhost via socket API (or other API)

Discussion in 'Python' started by Timothy Grant, Aug 5, 2008.

  1. On Tue, Aug 5, 2008 at 2:50 PM, David York <> wrote:
    > Does anybody know how to find the real IP address (e.g.: address visible to
    > internet) of a machine via Python? In other words I have a machine with an
    > IP address something like 192.168.1.5, an address given to me by a router.
    > The router's address (and thus my machine's address) to the outside world is
    > something realistic, 123.156.123.156 or whatever. How do I get that
    > number? I've tried socket.getaddrinfo('localhost', None) but all I get is
    > 127.0.0.1 as expected.
    >
    > How do I find out my machine's IP address as visible to the outside world?
    > Thanks a lot.
    >
    > David


    I'm not sure what you are trying to accomplish. The machine I'm typing
    this on has a 192.168.x.x number. The router that gave it to me also
    has a 192.168.x.x number. However, I know that that is not the IP that
    the world sees when my packets finally leave the building.

    What if your machine has multiple interface cards in it?

    What are you trying to accomplish?


    --
    Stand Fast,
    tjg. [Timothy Grant]
    Timothy Grant, Aug 5, 2008
    #1
    1. Advertising

  2. Timothy Grant schrieb:
    > On Tue, Aug 5, 2008 at 2:50 PM, David York <> wrote:
    >> Does anybody know how to find the real IP address (e.g.: address visible to
    >> internet) of a machine via Python? In other words I have a machine with an
    >> IP address something like 192.168.1.5, an address given to me by a router.
    >> The router's address (and thus my machine's address) to the outside world is
    >> something realistic, 123.156.123.156 or whatever. How do I get that
    >> number? I've tried socket.getaddrinfo('localhost', None) but all I get is
    >> 127.0.0.1 as expected.


    Try scraping a service like http://www.meineip.de/

    Diez
    Diez B. Roggisch, Aug 5, 2008
    #2
    1. Advertising

  3. Sorry for replying to the replier (Timothy) instead of the OP (David),
    but the original post seems to have been eaten by my ISP.

    On Tue, 05 Aug 2008 15:48:26 -0700, Timothy Grant wrote:

    > On Tue, Aug 5, 2008 at 2:50 PM, David York <> wrote:
    >> Does anybody know how to find the real IP address (e.g.: address
    >> visible to internet) of a machine via Python? In other words I have a
    >> machine with an IP address something like 192.168.1.5, an address given
    >> to me by a router. The router's address (and thus my machine's address)
    >> to the outside world is something realistic, 123.156.123.156 or
    >> whatever. How do I get that number? I've tried
    >> socket.getaddrinfo('localhost', None) but all I get is 127.0.0.1 as
    >> expected.
    >>
    >> How do I find out my machine's IP address as visible to the outside
    >> world? Thanks a lot.
    >>
    >> David

    >
    > I'm not sure what you are trying to accomplish. The machine I'm typing
    > this on has a 192.168.x.x number. The router that gave it to me also has
    > a 192.168.x.x number. However, I know that that is not the IP that the
    > world sees when my packets finally leave the building.


    That's the IP address the OP probably wants. At least, when I've asked
    this exact same question, that's what I meant.

    The only way I know of is to query an external server that will tell you.
    There's a few of them out there. Here's a few:

    http://checkip.dyndns.org/
    http://www.showmyip.com
    http://www.showmyip.com/simple/
    http://whatismyip.org/

    The basic algorithm is to connect to one of those sites and fetch the
    data it returns, then parse it appropriately. Some of them return a
    simple IP address, some a complicated bunch of text, some a nicely
    formatted XML document. Some of them only allow you to query the server a
    limited number of times. I don't remember which is which.

    To get you started, here's an untested piece of code:

    import urllib2
    import re
    data = urllib2.urlopen(site).read()
    matcher = re.compile(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
    ip_address = matcher.search(data).group()



    --
    Steven
    Steven D'Aprano, Aug 6, 2008
    #3
  4. Emile van Sebille, Aug 6, 2008
    #4
  5. Timothy Grant

    John Machin Guest

    On Aug 6, 9:51 am, Emile van Sebille <> wrote:
    > Steven D'Aprano wrote:
    > > The only way I know of is to query an external server that will tell you.
    > > There's a few of them out there. Here's a few:

    >
    > >http://checkip.dyndns.org/
    > >http://www.showmyip.com
    > >http://www.showmyip.com/simple/
    > >http://whatismyip.org/

    >
    > all four of these show my internal IP of 192.168.x.x in both ie and
    > firefox. Onlyhttp://www.meineip.de/shows the external IP.
    >


    All four of Steven's list showed my *external* address using Firefox,
    as did http://www.meineip.de/
    John Machin, Aug 6, 2008
    #5
  6. On Tue, Aug 5, 2008 at 4:39 PM, Steven D'Aprano
    <> wrote:
    > Sorry for replying to the replier (Timothy) instead of the OP (David),
    > but the original post seems to have been eaten by my ISP.
    >
    > On Tue, 05 Aug 2008 15:48:26 -0700, Timothy Grant wrote:
    >
    >> On Tue, Aug 5, 2008 at 2:50 PM, David York <> wrote:
    >>> Does anybody know how to find the real IP address (e.g.: address
    >>> visible to internet) of a machine via Python? In other words I have a
    >>> machine with an IP address something like 192.168.1.5, an address given
    >>> to me by a router. The router's address (and thus my machine's address)
    >>> to the outside world is something realistic, 123.156.123.156 or
    >>> whatever. How do I get that number? I've tried
    >>> socket.getaddrinfo('localhost', None) but all I get is 127.0.0.1 as
    >>> expected.
    >>>
    >>> How do I find out my machine's IP address as visible to the outside
    >>> world? Thanks a lot.
    >>>
    >>> David

    >>
    >> I'm not sure what you are trying to accomplish. The machine I'm typing
    >> this on has a 192.168.x.x number. The router that gave it to me also has
    >> a 192.168.x.x number. However, I know that that is not the IP that the
    >> world sees when my packets finally leave the building.

    >
    > That's the IP address the OP probably wants. At least, when I've asked
    > this exact same question, that's what I meant.
    >
    > The only way I know of is to query an external server that will tell you.
    > There's a few of them out there. Here's a few:
    >
    > http://checkip.dyndns.org/
    > http://www.showmyip.com
    > http://www.showmyip.com/simple/
    > http://whatismyip.org/
    >
    > The basic algorithm is to connect to one of those sites and fetch the
    > data it returns, then parse it appropriately. Some of them return a
    > simple IP address, some a complicated bunch of text, some a nicely
    > formatted XML document. Some of them only allow you to query the server a
    > limited number of times. I don't remember which is which.
    >
    > To get you started, here's an untested piece of code:
    >
    > import urllib2
    > import re
    > data = urllib2.urlopen(site).read()
    > matcher = re.compile(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
    > ip_address = matcher.search(data).group()
    >
    >
    >
    > --
    > Steven


    Now that is a cool solution to the problem. It even works in my work
    environment where I have three routers between me and the front door.

    --
    Stand Fast,
    tjg. [Timothy Grant]
    Timothy Grant, Aug 6, 2008
    #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. Jiong Feng
    Replies:
    0
    Views:
    817
    Jiong Feng
    Nov 19, 2003
  2. Alex
    Replies:
    4
    Views:
    565
    Hasani Blackwell
    Feb 8, 2005
  3. =?Utf-8?B?S2VuTGVl?=
    Replies:
    5
    Views:
    2,708
  4. EdwardH

    Localhost isn't localhost

    EdwardH, Sep 29, 2005, in forum: Java
    Replies:
    20
    Views:
    6,383
    Roedy Green
    Oct 1, 2005
  5. Jean-Paul Calderone
    Replies:
    0
    Views:
    966
    Jean-Paul Calderone
    Jan 27, 2009
Loading...

Share This Page