How can I use Python to get the system hostname?

PythonHostname

Python Problem Overview


I'm writing a chat program for a local network. I would like be able to identify computers and get the user-set computer name with Python.

Python Solutions


Solution 1 - Python

Use socket and its gethostname() functionality. This will get the hostname of the computer where the Python interpreter is running:

import socket
print(socket.gethostname())

Solution 2 - Python

Both of these are pretty portable:

import platform
platform.node()

import socket
socket.gethostname()

Any solutions using the HOST or HOSTNAME environment variables are not portable. Even if it works on your system when you run it, it may not work when run in special environments such as cron.

Solution 3 - Python

You will probably load the os module anyway, so another suggestion would be:

import os
myhost = os.uname()[1]

Solution 4 - Python

What about :

import platform

h = platform.uname()[1]

Actually you may want to have a look to all the result in platform.uname()

Solution 5 - Python

os.getenv('HOSTNAME') and os.environ['HOSTNAME'] don't always work. In cron jobs and WSDL, HTTP HOSTNAME isn't set. Use this instead:

import socket
socket.gethostbyaddr(socket.gethostname())[0]

It always (even on Windows) returns a fully qualified host name, even if you defined a short alias in /etc/hosts.

If you defined an alias in /etc/hosts then socket.gethostname() will return the alias. platform.uname()[1] does the same thing.

I ran into a case where the above didn't work. This is what I'm using now:

import socket
if socket.gethostname().find('.')>=0:
    name=socket.gethostname()
else:
    name=socket.gethostbyaddr(socket.gethostname())[0]

It first calls gethostname to see if it returns something that looks like a host name, if not it uses my original solution.

Solution 6 - Python

From at least python >= 3.3:

You can use the field nodename and avoid using array indexing:

os.uname().nodename

Although, even the documentation of os.uname suggests using socket.gethostname()

Solution 7 - Python

If I'm correct, you're looking for the socket.gethostname function:

>> import socket
>> socket.gethostname()
'terminus'

Solution 8 - Python

socket.gethostname() could do

Solution 9 - Python

On some systems, the hostname is set in the environment. If that is the case for you, the os module can pull it out of the environment via os.getenv. For example, if HOSTNAME is the environment variable containing what you want, the following will get it:

import os
system_name = os.getenv('HOSTNAME')

Update: As noted in the comments, this doesn't always work, as not everyone's environment is set up this way. I believe that at the time I initially answered this I was using this solution as it was the first thing I'd found in a web search and it worked for me at the time. Due to the lack of portability I probably wouldn't use this now. However, I am leaving this answer for reference purposes. FWIW, it does eliminate the need for other imports if your environment has the system name and you are already importing the os module. Test it - if it doesn't work in all the environments in which you expect your program to operate, use one of the other solutions provided.

Solution 10 - Python

I needed the name of the PC to use in my PyLog conf file, and the socket library is not available, but os library is.

For Windows I used:

os.getenv('COMPUTERNAME', 'defaultValue')

Where defaultValue is a string to prevent None being returned

Solution 11 - Python

You have to execute this line of code

sock_name = socket.gethostname()

And then you can use the name to find the addr :

print(socket.gethostbyname(sock_name))

Solution 12 - Python

To get fully qualified hostname use socket.getfqdn()

import socket

print socket.getfqdn()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohnView Question on Stackoverflow
Solution 1 - PythonAlexView Answer on Stackoverflow
Solution 2 - PythonrobertView Answer on Stackoverflow
Solution 3 - Pythonmike0042View Answer on Stackoverflow
Solution 4 - PythonLucien HercaudView Answer on Stackoverflow
Solution 5 - PythonTom EkbergView Answer on Stackoverflow
Solution 6 - PythonShubham ChaudharyView Answer on Stackoverflow
Solution 7 - PythonterminusView Answer on Stackoverflow
Solution 8 - Pythonvpit3833View Answer on Stackoverflow
Solution 9 - PythonGreenMattView Answer on Stackoverflow
Solution 10 - PythonBill KiddView Answer on Stackoverflow
Solution 11 - PythonParzifal KaliView Answer on Stackoverflow
Solution 12 - PythonRishi BansalView Answer on Stackoverflow