Fix Python – Getting name of windows computer running python script?

Question

Asked By – Eric Palakovich Carr

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?

Now we will see solution for issue: Getting name of windows computer running python script?


Answer

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']
'DARK-TOWER'

This question is answered By – Eric Palakovich Carr

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0