Python: Get the name of the host on which the routine is running
Python Basic: Exercise-100 with Solution
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
Python Code Editor:
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.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/python-basic-exercise-100.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics