50

I know I can use platform.node() to get my computer's network name:

>>> import platform
>>> platform.node()
'MyComputerName'

But what I really want is something that will work similar to the following:

>>> get_full_network_domain_name()
'MyComputerName.it.na.mycompany.com'

Does something like this exist?

1 Answer 1

70

The fully qualified domain name is returned by socket.getfqdn().

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, "fully qualified domain name" was the phrase I was looking for!
For me this doesn't yield the same result as hostname -f on the shell. So be careful.
@semente , hostname -f does it like this: socket.getaddrinfo(socket.gethostname(), 0, flags=socket.AI_CANONNAME)[0][3] and getfqdn() seems to lookup the hostname in /etc/hosts and uses the first entry which contains a dot. For me the hostname -f returns 'daniel-thinkpad' and getfqdn() returns localhost6.localdomain6
@DaniëlvanEeden: Thanks! However, my version of getaddrinfo doesn't support keyword arguments, so I had to use socket.getaddrinfo(socket.gethostname(), 0, 0, 0, 0, socket.AI_CANONNAME)[0][3] instead.
This does not work reliably at all. Half the time it comes back with just the hostname when I see that the hostname it comes back with is easily looked up by nslookup and comes back with an fqdn.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.