import socket


def GetHostName(useFqdn=False):
  """Returns the host name, and with FQDN if specified."""
  thisHost = "localhost"
  
  if useFqdn:
    thisHost = socket.getfqdn()
    # If the server is configured correctly getfqdn() works fine, but it might
    # take what's in /etc/hosts first, so double check to see if
    # hostname is more appropriate to avoid using localhost or something else
  
    if thisHost == "localhost" or thisHost == "127.0.0.1" or thisHost == "127.0.1.1" or "." not in thisHost:
      thisHost = socket.gethostname()
    
  else:
    # hostname itself could be set to FQDN too, so it could be the same as above
    thisHost = socket.gethostname()

  return thisHost