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?
In order to get the hostname of a computer, you can use socket and its gethostname() functionality. The gethostname() return a string containing the hostname of the machine where the Python interpreter is currently executing.
Use the key combination [Windows] + [R] to open the Run window. Type in “cmd” and press the Enter key. Type “hostname” into the field and then press Enter again. The computer name will appear.
you can use os. uname() to get a better feel for what kind of system it is.
You can use psutil. Process(). cmdline() to see the complete command line of a process. Alternatively, you could lock the files you're working on.
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'
import socket socket.gethostname()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With