how to obtain its ip address ?

Discussion in 'Python' started by marco, Jun 23, 2004.

  1. marco

    marco Guest

    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
    marco, Jun 23, 2004
    #1
    1. Advertising

  2. marco

    Peter Hansen Guest

    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
    Peter Hansen, Jun 23, 2004
    #2
    1. Advertising

  3. marco

    Daniel Yoo Guest

    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) ?!


    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/Webware-0.8b2/MiscUtils/Docs/Source/Files/Funcs.py.html

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


    Good luck to you!
    Daniel Yoo, Jun 23, 2004
    #3
  4. marco

    Lee Harr Guest

    On 2004-06-23, 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) ?!
    >



    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'
    Lee Harr, Jun 23, 2004
    #4
  5. In article <>,
    Peter Hansen <> 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 <>
    Business: http://www.Phaseit.net
    Cameron Laird, Jun 23, 2004
    #5
  6. marco

    Peter Hansen Guest

    Cameron Laird wrote:

    > In article <>,
    > Peter Hansen <> 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...
    Peter Hansen, Jun 23, 2004
    #6
  7. marco <> wrote in message news:<40d9c75d$0$26573$>...
    > 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
    Michele Simionato, Jun 24, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TC
    Replies:
    0
    Views:
    707
  2. candide
    Replies:
    65
    Views:
    1,369
  3. thunk
    Replies:
    1
    Views:
    292
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    454
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    599
    thunk
    Apr 3, 2010
Loading...

Share This Page