Entry
Fully qualified hostname
Jul 5th, 2000 10:00
Nathan Wallace, Hans Nowak, Snippet 137, Jonathan Giddy
"""
Packages: networking.sockets
"""
"""
>> >What is the best way to get the fully qualified hostname in python (ie
>> >hostname.domainname) ?
>> >
>> >A portable solution would be ideal, but I would settle for Unix only.
Under Unix, I get the best (i.e. least surprising) results using the
following gethostname function:
"""
import socket
def getdnsnames(name):
d = socket.gethostbyaddr(name)
names = [ d[0] ] + d[1] + d[2]
return names
def resolve(name):
names = getdnsnames(name)
for dnsname in names:
if '.' in dnsname:
fullname = dnsname
break
else:
fullname = name
return fullname
def gethostname():
fullname = socket.gethostname()
if '.' not in fullname:
fullname = resolve(fullname)
return fullname