~1 min read • Updated Oct 1, 2025
Program Overview
This Python program displays the name of the host machine that is currently running the script.
It uses the socket module to retrieve the hostname.
Python Code:
import socket
# Get and display the hostname
hostname = socket.gethostname()
print("\n--- Host Information ---")
print(f"Current Hostname: {hostname}")
Sample Output:
--- Host Information ---
Current Hostname: Shahin-Dev-Machine
Step-by-Step Explanation:
- The socket.gethostname() function retrieves the system’s hostname
- The result is printed in a clean format
- This is useful for identifying machines in a network or during remote execution
Written & researched by Dr. Shahin Siami