The fully qualified domain name is returned by socket.getfqdn().
BUT: it does not do the same as "hostname --fqdn".
There are some hints in the comments of the following question, but I would like to know the canonical answer. How do I get my computer's fully qualified domain name in Python?
How to get the FQDN like hostname --fqdn does.
To put it all together:
Python3:
import socket
socket.getaddrinfo(socket.gethostname(), 0, flags=socket.AI_CANONNAME)[0][3]
Python2:
import socket
socket.getaddrinfo(socket.gethostname(), 0, 0, 0, 0, socket.AI_CANONNAME)[0][3]
Credits: How do I get my computer's fully qualified domain name in Python?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With