Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to obtain its ip address ?

Reply
Thread Tools

how to obtain its ip address ?

 
 
marco
Guest
Posts: n/a
 
      06-23-2004
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
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      06-23-2004
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
 
Reply With Quote
 
 
 
 
Daniel Yoo
Guest
Posts: n/a
 
      06-23-2004
marco <(E-Mail Removed)> 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!
 
Reply With Quote
 
Lee Harr
Guest
Posts: n/a
 
      06-23-2004
On 2004-06-23, marco <(E-Mail Removed)> 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'

 
Reply With Quote
 
Cameron Laird
Guest
Posts: n/a
 
      06-23-2004
In article <(E-Mail Removed)>,
Peter Hansen <(E-Mail Removed)> 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 <(E-Mail Removed)>
Business: http://www.Phaseit.net
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      06-23-2004
Cameron Laird wrote:

> In article <(E-Mail Removed)>,
> Peter Hansen <(E-Mail Removed)> 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...
 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      06-24-2004
marco <(E-Mail Removed)> wrote in message news:<40d9c75d$0$26573$(E-Mail Removed)>...
> 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
 
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: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
obtaining the IP ADDRESS of an IP POHNE by its MAC ADDRESS ProgDario Cisco 17 05-06-2005 02:32 PM



Advertisments