#!/usr/bin/env python import os def experiment(): """ Return the name of the local machine. Tries a number of methods to get a name other than 'localhost'. """ import socket import platform print("Test of $HOSTNAME") name = os.getenv('HOSTNAME') if name == None : print('Result of os.getenv is None') print(name) print() print("Test of platform.uname()[1]") name = platform.uname()[1] print(name) print() print("Test of name=socket.gethostname()") if socket.gethostname().find('.')>=0: name=socket.gethostname() print(name) print() print("Test of socket.gethostbyaddr(socket.gethostname())[0]") name=socket.gethostbyaddr(socket.gethostname())[0] print(name) print() return name def LocalHostname(): """ Return the name of the local machine. Tries a number of methods to get a name other than 'localhost' or a null result. """ import socket import platform def CheckName(name) : if name == None or name.startswith("localhost") or name == "" : OKAY = False else : OKAY = True return OKAY name = os.getenv('HOSTNAME') if not CheckName(name) : name = platform.uname()[1] if not CheckName(name) : if socket.gethostname().find('.')>=0: name=socket.gethostname() else: name=socket.gethostbyaddr(socket.gethostname())[0] return name print(LocalHostname())