140

I have a couple Windows computers on my network that will be running a python script. A different set of configuration options should be used in the script depending on which computer is running this script.

How would I get that computer name in the python script?

Let's say the script was running on a computer named DARK-TOWER, I'd like to write something like this:

>>> python.library.get_computer_name()
'DARK-TOWER'

Is there a standard or third party library I can use?

6 Answers 6

235

It turns out there are three options (including the two already answered earlier):

>>> import platform
>>> import socket
>>> import os
>>> platform.node()
'DARK-TOWER'
>>> socket.gethostname()
'DARK-TOWER'
>>> os.environ['COMPUTERNAME'] # WORK ONLY ON WINDOWS
'DARK-TOWER'
Sign up to request clarification or add additional context in comments.

10 Comments

Answering one's own question may be considered poor taste by some, but it is perfectly fine, as per the FAQ: stackoverflow.com/faq
I don't think it's bad at all, since Eric was compiling a few different responses into a single resource, not to mention adding a new one (platform).
ok, so this cries out for a follow-up: what's the difference between platform.node() and socket.gethostname() ? can they ever be different?
On posix systems, socket.gethostname() returns the libc gethostname(), while platform.node() returns platform.uname()[1] which is somewhat related to os.uname() which calls the libc uname()... It looks like they might end up in the same place, but they take quite different paths to get there and I wouldn't be relying on the value to be the same across different platforms.
Worth noting that os.environ['COMPUTERNAME'] will return all uppercase, whereas platform.node() and socket.gethostname() can return mixed case.
|
52
import socket
socket.gethostname()

4 Comments

+1 for this being the best way to go. It's the most effective cross-platform call.
platform.node() is crossplatform too
Computer name and host name are two different things. For example, when you are in a VPN, your host name is X and your computer name can be Y. This answer is not correct, since the question is about COMPUTER name and not host name.
this works for me, while using os doesn't
25

From https://mail.python.org/pipermail/python-list/2006-April/397494.html

import os
os.getenv('COMPUTERNAME')

3 Comments

This doesn't work for me on OS-X 10.6.8 using Python 2.7.2, it returns None. The other methods described do work however.
@PeterGibson I had the same thing occurring to me on Ubuntu 12.04.
Returns None for me on Ubuntu 16.04. Interestingly, HOSTNAME is a defined environment variable in bash, but both os.getenv('HOSTNAME') returns None also, while socket.gethostname() returns the correct string.
18

As Eric Palakovich Carr said you could use these three variants.

I prefer using them together:

def getpcname():
    n1 = platform.node()
    n2 = socket.gethostname()
    n3 = os.environ["COMPUTERNAME"]
    if n1 == n2 or n1 == n3:
        return n1
    elif n2 == n3:
        return n2
    else:
        raise Exception("Computernames are not equal to each other")

I prefer it when developing cross patform applications to be sure ;)

4 Comments

Just a bit cleaner: if n1==n2 or n2==n3: return n1 elif n2==n3: return n2 else: raise Exception("Computer names are not equal to each other")
@CharlesPlager It should be: if n1==n2 or n1==n3: return n1 elif n2==n3: return n2 else: raise Exception("Computer names are not equal to each other") (n1 instead of n2)
@dexteritas: You are correct. (It won't let me edit it for whatever reason).
You could use os.getenv("COMPUTERNAME") which returns None instead of raising error if "COMPUTERNAME" does not exist in env.
8

Since the python scrips are for sure running on a windows system, you should use the Win32 API GetComputerName or GetComputerNameEx

You can get the fully qualified DNS name, or NETBIOS name, or a variety of different things.

import win32api
win32api.GetComputerName()

>>'MYNAME'

Or:

import win32api
WIN32_ComputerNameDnsHostname = 1 
win32api.GetComputerNameEx(WIN32_ComputerNameDnsHostname)

>> u'MYNAME'

Comments

3

I bet gethostname will work beautifully.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.