Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: FQDN like `hostname --fqdn`?

Tags:

python

fqdn

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.

like image 642
guettli Avatar asked Feb 13 '26 08:02

guettli


1 Answers

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?

like image 98
Adrian W Avatar answered Feb 15 '26 20:02

Adrian W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!