Skip to content

Instantly share code, notes, and snippets.

@leksak
Last active December 30, 2015 08:02
Show Gist options
  • Save leksak/da024522db2ffc637522 to your computer and use it in GitHub Desktop.
Save leksak/da024522db2ffc637522 to your computer and use it in GitHub Desktop.
Get the fully qualified domain name of the host where the function is executed.
#!/usr/bin/env python
"""
The above is the most portable shebang,
#!/usr/bin/env python
is more portable because in general the program /usr/bin/env can be
used to "activate" the desired command without full path.
Otherwise, you would have to specify the full path of the Python
interpreter, which can vary.
So no matter if the Python interpreter was in /usr/bin/python or in
/usr/local/bin/python or in your home directory, using
#!/usr/bin/env python will work.
"""
import socket
def get_fully_qualified_domain_name():
"""Get the fully qualified domain name of the host where the function is executed.
A device with the hostname myhost and the parent domain name
example.com has the fully qualified domain name (FQDN)
myhost.example.com. The FQDN uniquely distinguishes the device from
any other hosts called myhost in other domains.
See: https://en.wikipedia.org/wiki/Fully_qualified_domain_name
"""
return socket.gethostbyaddr(socket.gethostname())[0]
print get_fully_qualified_domain_name()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment