Skip to content

How to find hostname using Python

How to find hostname using Python
Share

Reading Time: 2 minutes

How to find hostname using Python

This tutorial will guide you on How to find hostname using Python. You can use any one module to get the hostname. “platform” and “socket” are two modules that can be used to get the hostname. Discovering your system’s hostname in Python is a straightforward process, and you have two versatile modules to achieve this – “platform” and “socket”. This tutorial guides you through obtaining the hostname using both modules. We have published a detailed article on How to change hostname in Linux

  • You can get the hostname using both the platform and socket module
  • The first example is to get the node name using the platform module
  • You need to install Jupyter notebook and Python to proceed further.

Python Code – Platform Module

import platform  # Import Module
platform.node()  # Get node /host Name
How to find hostname using Python

Python Code – Socket Module

import socket #import socket module
print(socket.gethostname()) # This will print hostname 

Q: How can I find the hostname using Python?

A: You can find the hostname using two Python modules – “platform” and “socket.”

Python Code – Platform Module:

pythonCopy code

import platform # Import Module platform.node() # Get node / host Name

Q: How do I find the hostname using the platform module in Python?

A: Use the following Python code:

pythonCopy code

import platform # Import Module platform.node() # Get node / host Name

Python Code – Socket Module:

pythonCopy code

import socket # Import socket module print(socket.gethostname()) # Print hostname

Q: What’s the Python code to find the hostname using the socket module?

A: Use this code:

pythonCopy code

import socket # Import socket module print(socket.gethostname()) # Print hostname

You can learn “How to install PIP” as well.

How to install Juptyer Notebook on Windows

You can read more about the platform module on website


Share

Leave a Reply

Your email address will not be published. Required fields are marked *

?>