Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the difference between gethostname and getfqdn?

Tags:

python

sockets

Just the title, what the difference between them? In python, socket.gethostbyname(socket.gethostname()) and socket.gethostbyname(socket.getfqdn()) return different results on my computer.

like image 529
abcazx Avatar asked Dec 18 '12 11:12

abcazx


People also ask

What is socket Getfqdn?

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.

What does socket gethostname return?

The gethostname function returns the name of the local host into the buffer specified by the name parameter. The host name is returned as a null-terminated string. The form of the host name is dependent on the Windows Sockets provider—it can be a simple host name, or it can be a fully qualified domain name.


1 Answers

From documentation,

socket.gethostname returns a string containing the hostname of the machine where the Python interpreter is currently executing.

socket.getfqdn returns a fully qualified domain name if it's available or gethostname otherwise.

Fully qualified domain name is a domain name that specifies its exact location in the tree hierarchy of the DNS. From wikipedia examples:

For example, given a device with a local hostname myhost and a parent domain name example.com, the fully qualified domain name is myhost.example.com.

like image 111
Alexey Kachayev Avatar answered Nov 11 '22 09:11

Alexey Kachayev