Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.getfqdn() and socket.gethostname() are giving Different IP addresses when using socket.gethostname

Tags:

python

sockets

I am running a Virtual Machine which gives the following values and when the code is run (by doing some action on the website)

socket.getfqdn()
x-vps-01.abc.it

socket.gethostname()
x-vps-01

socket.gethostbyname(socket.getfqdn())
216.185.103.35

socket.gethostbyname(socket.gethostname())
78.47.171.19

Please note that, when I log into the terminal and run the above, I always get 78.47.171.19

like image 677
Jamal Avatar asked Nov 09 '12 14:11

Jamal


1 Answers

I tried what you did on a Linux box. Maybe you should state what OS and network situation you are running on.

Using strace I found that socket.getfqdn() is using information provided in the file /etc/hosts while socket.gethostname() only prints data from the result of the system call uname(); basically you could say the one asks the network module while the other asks the kernel. Both have an answer to your question but they do not necessarily match because they have different views on that matter.

Calling socket.gethostbyname() also queries the network module (searches the contents of the file /etc/hosts for a match in my case). Giving the answer of the kernel to the network function isn't really what you should do. In most cases this will work nevertheless. You found a spot in which it produced strange results.

like image 166
Alfe Avatar answered Oct 04 '22 04:10

Alfe