3

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.

guettli
  • 25,042
  • 81
  • 346
  • 663

1 Answers1

4

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?

Adrian W
  • 4,563
  • 11
  • 38
  • 52