3
votes

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.

1

1 Answers

4
votes

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?