473,473 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
5 2037
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: crypto_stonelock | last post by:
Hello, I was wondering if anybody knew how to retrieve the IP address attributed dynamically by your ISP without having to use the info collected on a socket with say .getLocalAddress. I'm...
1
by: perspolis | last post by:
Hi all How can I find the address that user type in address bar?? I have some subdomains that have a homepage.I want to determine which address is user typed and based on that address load...
1
by: Ezz | last post by:
I have an interesting problem...and its one of those where I know what I want but don't know how to ask it =) I have an eCommerce application that uses Paypal for its payment gateway. It is an...
1
by: Phill W. | last post by:
For my sins, I've been given a program to write that uses the Framework Sockets classes. Needless to say, I'm getting problems as I'm going along, but by far my biggest woe is MSDN! Virtually...
5
by: DaTurk | last post by:
I have this application that has one sender, and multiple receivers, the receivers are subscribing to a multicast group to which the sender is sending information synchronously, while the receivers...
0
by: Christian Heimes | last post by:
David York wrote: Most modern routers home routers support the IGD part of UPnP (http://en.wikipedia.org/wiki/Internet_Gateway_Device_Protocol). You can use an UPnP client software to query the...
0
by: =?Utf-8?B?UmFqbmk=?= | last post by:
Dear William Stacey, I have written a server code using the Windows Socket API's. Wherein I have created the socket and bound it to a particular IP address and port number. Later I have made the...
0
Airslash
by: Airslash | last post by:
Hello, not really sure if this will lead to something, but I just want to have a small discussion about the Berkley Sockets API. Recently i've been working on TCP Client/Server architectures, and...
0
by: Andreas Englund | last post by:
Hello, I need to communication with an ERP using sockets (API-calls) from a .NET website and I am wondering where to search for information on how to work with connection pooling within IIS. Is...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.