socket.gethostbyname returns `wrong` result?

Discussion in 'Python' started by Frank Miles, Jul 26, 2004.

  1. Frank Miles

    Frank Miles Guest

    Hi everyone...

    I'm trying to use the IP address as a way of detecting the current state
    of my laptop. [Using laptop-net, it senses what network it's connected
    to, and configures various parameters including IP address. This seems
    to work properly, as reflected in the `ifconfig` output.]

    Unfortunately while the simple script:

    import socket
    print socket.gethostbyname(socket.gethostname())

    works on my desktop machines and server, on the laptop it gives:
    127.0.0.1
    which, of course, doesn't provide any idea as to the state of the network.

    Is there any way to coerce the external IP#? Or another way of accessing
    this number?

    TIA for any tips or pointers to relevant docs!

    -frank
    --
     
    Frank Miles, Jul 26, 2004
    #1
    1. Advertising

  2. Frank Miles wrote:

    > import socket
    > print socket.gethostbyname(socket.gethostname())
    >
    > works on my desktop machines and server, on the laptop it gives:
    > 127.0.0.1
    > which, of course, doesn't provide any idea as to the state of the network.


    Do you have, by any chance, an entry in your hosts file like this:

    127.0.0.1 <real hostname>

    If so, remove that line! The hosts file should only contain :

    127.0.0.1 localhost

    for the 127.0.0.1 address.

    -Irmen
     
    Irmen de Jong, Jul 26, 2004
    #2
    1. Advertising

  3. "Frank Miles" <> wrote in message
    news:ce40kn$9sm$...
    > Unfortunately while the simple script:
    >
    > import socket
    > print socket.gethostbyname(socket.gethostname())
    >
    > works on my desktop machines and server, on the laptop it gives:
    > 127.0.0.1
    > which, of course, doesn't provide any idea as to the state of the network.
    >
    > Is there any way to coerce the external IP#? Or another way of accessing
    > this number?
    >
    > TIA for any tips or pointers to relevant docs!


    A couple of things spring to mind.

    Maybe 'hostname' is defined in your hosts file as '127.0.0.1' (should be
    'localhost' or sometimes 'loopback').

    Are you sure that DNS is setup correctly? nslookup, ping and the like work
    ok outside python (and show the correct IP)?
     
    George Kinney, Jul 27, 2004
    #3
    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. Eric Brunel
    Replies:
    0
    Views:
    1,461
    Eric Brunel
    Oct 27, 2003
  2. Fortepianissimo

    timeout in socket.gethostbyname()?

    Fortepianissimo, Dec 7, 2003, in forum: Python
    Replies:
    1
    Views:
    1,367
    Fortepianissimo
    Dec 8, 2003
  3. Laszlo Nagy
    Replies:
    1
    Views:
    4,861
    Mark Wooding
    Jan 27, 2009
  4. CarlosRivera

    Socket::gethostbyname()

    CarlosRivera, Oct 3, 2004, in forum: Ruby
    Replies:
    5
    Views:
    217
    Gavin Sinclair
    Oct 4, 2004
  5. Michael Tan
    Replies:
    32
    Views:
    997
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page