socket.gethostbyname returns `wrong` result?

F

Frank Miles

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
--
 
I

Irmen de Jong

Frank said:
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
 
G

George Kinney

Frank Miles said:
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)?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top