Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting name of windows computer running python script?

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?

like image 883
Eric Palakovich Carr Avatar asked Apr 28 '09 20:04

Eric Palakovich Carr


People also ask

How do I get the current computer name in Python?

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.

How do I find out my computer's Run name?

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.

How do I get operating system details in Python?

you can use os. uname() to get a better feel for what kind of system it is.

How can I tell if Python script is running on Windows?

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.


2 Answers

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' 
like image 167
Eric Palakovich Carr Avatar answered Nov 15 '22 22:11

Eric Palakovich Carr


import socket socket.gethostname() 
like image 45
brettkelly Avatar answered Nov 15 '22 23:11

brettkelly