Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   how to obtain its ip address ? (http://www.velocityreviews.com/forums/t332457-how-to-obtain-its-ip-address.html)

marco 06-23-2004 06:09 PM

how to obtain its ip address ?
 
I'd search a lot ... but i had not found
how can i obtain my ip address (when i'm connected to the www) ?!

thanks a lot for any tips
marco

Peter Hansen 06-23-2004 08:36 PM

Re: how to obtain its ip address ?
 
marco wrote:

> I'd search a lot ... but i had not found
> how can i obtain my ip address (when i'm connected to the www) ?!


You can probably search the archives for this list/newsgroup to
find more complete answers (and hints about how else to solve it),
but the short answer is "you can't".

The longer answer is that (a) machines can have multiple addresses
so you have to take that into account in the general case, (b)
there is no portable way to do it (and you didn't specify what
platform you are on), and (c) it's generally not necessary anyway,
though there are a few valid usecases... you might even have one.

On Windows you would have to parse the output of the "ipconfig"
command using os.popen or something like that. On Linux the
command is "ifconfig", but the output is totally different.

Provide a little more info about your situation and needs and
you might get a more useful answer.

-Peter

Daniel Yoo 06-23-2004 08:42 PM

Re: how to obtain its ip address ?
 
marco <manatlan@online.fr> wrote:
: I'd search a lot ... but i had not found
: how can i obtain my ip address (when i'm connected to the www) ?!


Hi Marco,


Here's a quicky way to do it:

###
import socket

def getSelfIp():
"""Returns the IP address of the local host."""
return socket.gethostbyname(socket.gethostname())
###


This might not work perfectly. If you want something more
comprehensive, you may want to use something like the localIP()
function in Webware:

http://webware.sourceforge.net/Webwa.../Funcs.py.html

This appears to be a bit more systematic in getting a good local IP
address.


Good luck to you!

Lee Harr 06-23-2004 09:03 PM

Re: how to obtain its ip address ?
 
On 2004-06-23, marco <manatlan@online.fr> wrote:
> I'd search a lot ... but i had not found
> how can i obtain my ip address (when i'm connected to the www) ?!
>



Probably it will depend somewhat on your operating system.
This works for me ...

>>> import socket
>>> socket.gethostbyname('localhost')

'127.0.0.1'
>>> hostname = socket.gethostname()
>>> hostname

'homer'
>>> socket.gethostbyname(hostname)

'172.16.0.5'


Cameron Laird 06-23-2004 09:28 PM

Re: how to obtain its ip address ?
 
In article <H-ednTX8EdQsdETdRVn-gg@powergate.ca>,
Peter Hansen <peter@engcorp.com> wrote:
>marco wrote:
>
>> I'd search a lot ... but i had not found
>> how can i obtain my ip address (when i'm connected to the www) ?!

>
>You can probably search the archives for this list/newsgroup to
>find more complete answers (and hints about how else to solve it),
>but the short answer is "you can't".
>
>The longer answer is that (a) machines can have multiple addresses

.
.
.
Even more details appear at <URL:
http://wiki.tcl.tk/MyOwnIPAddress >.
--

Cameron Laird <claird@phaseit.net>
Business: http://www.Phaseit.net

Peter Hansen 06-23-2004 10:06 PM

Re: how to obtain its ip address ?
 
Cameron Laird wrote:

> In article <H-ednTX8EdQsdETdRVn-gg@powergate.ca>,
> Peter Hansen <peter@engcorp.com> wrote:
>
>>marco wrote:
>>
>>
>>>I'd search a lot ... but i had not found
>>>how can i obtain my ip address (when i'm connected to the www) ?!

>>
>>You can probably search the archives for this list/newsgroup to
>>find more complete answers (and hints about how else to solve it),
>>but the short answer is "you can't".
>>
>>The longer answer is that (a) machines can have multiple addresses

> .
> Even more details appear at <URL:
> http://wiki.tcl.tk/MyOwnIPAddress >.


Thanks Cameron. That's a pretty good page (especially if you like
TCL), and it doesn't even get into issues relating to SNAT/DNAT,
routers, firewalls, and the like, which many of us are behind these
days...

Michele Simionato 06-24-2004 04:27 AM

Re: how to obtain its ip address ?
 
marco <manatlan@online.fr> wrote in message news:<40d9c75d$0$26573$626a14ce@news.free.fr>...
> I'd search a lot ... but i had not found
> how can i obtain my ip address (when i'm connected to the www) ?!
>
> thanks a lot for any tips
> marco


In may case on linux this worked:

$ wget -q "http://ipid.shat.net/iponly" -O - |sed -n 3p

This gives your address as seen from outside, which is probably
what you want, whereas other solutions I tried were giving me
my local address on the LAN.

Michele Simionato


All times are GMT. The time now is 02:52 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.