sorin
sorin

Reputation: 170310

How can I get the default machine domain name in Python using a portable way?

I am looking for a Python solution for obtaining the current machine default domain name, one that will work on Linux, Mac OS X and Windows.

So far I discovered that:

Upvotes: 3

Views: 3423

Answers (2)

rasebo
rasebo

Reputation: 1008

Try:

#!/usr/bin/env python

import socket

def get_dns_domain():
    return socket.getfqdn().split('.', 1)[1]

print (get_dns_domain()) # to match python>2.7

Upvotes: 4

Nikita
Nikita

Reputation: 6331

Try:

from socket import gethostname
machine_name=gethostname()
print(machine_name)

Upvotes: 1

Related Questions