Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Finding IP address of localhost via socket API (or other API)

Reply
Thread Tools

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

 
 
Timothy Grant
Guest
Posts: n/a
 
      08-05-2008
On Tue, Aug 5, 2008 at 2:50 PM, David York <(E-Mail Removed)> 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]
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      08-05-2008
Timothy Grant schrieb:
> On Tue, Aug 5, 2008 at 2:50 PM, David York <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      08-05-2008
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 <(E-Mail Removed)> 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
 
Reply With Quote
 
Emile van Sebille
Guest
Posts: n/a
 
      08-05-2008
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. Only http://www.meineip.de/ shows the external IP.

Emile

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      08-06-2008
On Aug 6, 9:51 am, Emile van Sebille <(E-Mail Removed)> 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/


 
Reply With Quote
 
Timothy Grant
Guest
Posts: n/a
 
      08-06-2008
On Tue, Aug 5, 2008 at 4:39 PM, Steven D'Aprano
<(E-Mail Removed)> 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 <(E-Mail Removed)> 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]
 
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
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM
change from "//localhost:numbers/" to just "//localhost/"? =?Utf-8?B?S2VuTGVl?= ASP .Net 5 02-12-2006 05:14 AM
Localhost isn't localhost EdwardH Java 20 10-01-2005 12:08 AM
Can I Reset the Root from localhost/myproject/ to localhost/ Alex ASP .Net 4 02-08-2005 01:45 PM
HttpModule -- how to intercept urls like http://localhost/abc/def or http://localhost/abc/def/ where abc, def are non virtual dir Jiong Feng ASP .Net 0 11-19-2003 05:29 AM



Advertisments