GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python: Get the name of the host on which the routine is running - w3resource

Python: Get the name of the host on which the routine is running

Last update on May 17 2025 05:43:40 (UTC/GMT +8 hours)


Get Host Name

Write a Python program to get the name of the host on which the routine is running.

Sample Solution-1:

Python Code:

# Import the 'socket' module to work with networking functionalities.

import socket
# Use 'socket.gethostname()' to retrieve the name of the current host or machine.
host_name = socket.gethostname()

# Print the host name to the console.
print("Host name:", host_name)

Sample Output:

Host name: 0c299cb8f897

Sample Solution-2:

Python Code:

# Import the 'platform' module to retrieve system-related information.
import platform
# Use 'platform.uname()' to obtain a tuple of information about the system, including the host name.
host_name = platform.uname()[1]

# Print the host name to the console.
print("Host name:", host_name)

Sample Output:

Host name: 4735090b6baa

Sample Solution-3:

Python Code:

# Import the 'os' module to access operating system-related functionality.
import os
# Use 'os.uname().nodename' to retrieve the host name of the current system.
host_name = os.uname().nodename
# Print the host name to the console.
print("Host name:", host_name)

Sample Output:

Host name: 0c299cb8f897

For more Practice: Solve these Related Problems:

  • Write a Python program to retrieve both the hostname and IP address of the system.
  • Write a Python program to get the hostnames of all network interfaces available on a system.
  • Write a Python program to resolve a given domain name to its IP address.
  • Write a Python program to find the hostname of a remote server using its IP address.

Go to:

  • Python Basic Exercises Home ↩
  • Python Exercises Home ↩

Previous: Write a Python program to clear the screen or terminal.
Next: Write a Python program to access and print a URL's content to the console.

Python Code Editor:

 

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.