By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,761 Members | 1,199 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,761 IT Pros & Developers. It's quick & easy.

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

P: n/a
On Tue, Aug 5, 2008 at 2:50 PM, David York <da********@gmail.comwrote:
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]
Aug 5 '08 #1
Share this Question
Share on Google+
5 Replies

P: n/a
Timothy Grant schrieb:
On Tue, Aug 5, 2008 at 2:50 PM, David York <da********@gmail.comwrote:
>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
Aug 5 '08 #2

P: n/a
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 <da********@gmail.comwrote:
>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
Aug 5 '08 #3

P: n/a
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

Aug 5 '08 #4

P: n/a
On Aug 6, 9:51 am, Emile van Sebille <em...@fenx.comwrote:
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/
Aug 6 '08 #5

P: n/a
On Tue, Aug 5, 2008 at 4:39 PM, Steven D'Aprano
<st***@remove-this-cybersource.com.auwrote:
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 <da********@gmail.comwrote:
>>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]
Aug 6 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.