Reputation: 387
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
Upvotes: 9
Views: 23176
Reputation: 170340
I think that this issue is caused by Python bug specific to MacOS, the one that I mention at https://stackoverflow.com/a/53143006/99834
Good news: there is a workaround you can run on your machine.
Upvotes: 0
Reputation: 59416
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.
Upvotes: 13