;

How to Find the Hostname of a Computer in Python


Tutorialsrack 31/07/2020 Python

In this article, you will learn how to find the hostname of the computer in Python. There are many ways to find the hostname in python. 

Here are some examples to find the hostname of a computer in python. 

Example 1: Using socket.gethostname() method

In this example, you need to import the socket module from the Python Standard Library and use the socket.gethostname() method to find the hostname of a computer.

Here is the source code of the program to find the Hostname of the local computer in python

Example 1: Using socket.gethostname() method
# Find the Hostname of a Computer using socket.gethostname() in Python

#Import Module
import socket

# Print Output
print("HostName: ",socket.gethostname())

Example 2: Using socket.gethostbyaddr() Method

In this example, first, you need to import the socket module from the Python Standard Library and use the socket.gethostbyaddr() method and pass socket.gethostname() as a parameter to the socket.gethostbyaddr() method to find the hostname of a computer.

Here is the source code of the program to find the Hostname of the local computer in python.

Example 2: Using socket.gethostbyaddr() Method
# Find the Hostname of a Computer using socket.gethostbyaddr() in Python

#Import Module
import socket

# Print Output
print("HostName: ",socket.gethostbyaddr(socket.gethostname())[0])

Example 3: Using platform.uname()[1] Method

In this example, first, you need to import the  platform module from the Python Standard Library and use the platform.uname()[1] method to find the hostname of a computer.

Here is the source code of the program to find the Hostname of the local computer in python.

Example 3: Using platform.uname()[1] Method
# Find the Hostname of a Computer using  platform module and platform.uname()[1] in Python

# Import Module
import platform

# Print Output
print("HostName: ",platform.uname()[1])

Example 4: Using platform.node() Method

In this example, you need to import the platform module from the Python Standard Library and use the platform.node() method to find the hostname of a computer.

Here is the source code of the program to find the Hostname of the local computer in python.

Example 4: Using platform.node() Method
# Find the Hostname of a Computer using  platform module and platform.node() method in Python

# Import Module
import platform

# Print Output
print("HostName: ",platform.node())

Example 5: Using os.getenv() Method

In this example, you need to import the os module from the Python Standard Library and use the os.getenv() method to find the hostname of a computer. For Windows, you can use this example.

Here is the source code of the program to find the Hostname of the local computer in python.

Example 5: Using os.getenv() Method
# Find the Hostname of a Computer using os module and os.getenv() method in Python

# Import Module
import os

# Print Output
print("HostName: ",os.getenv('COMPUTERNAME', 'defaultValue'))

Where defaultValue is a string to prevent None being returned.

I hope this article will help you to understand how to find the hostname of the computer in Python.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags