w3resource

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 socket
host_name = socket.gethostname()
print("Host name:", host_name)

Sample Output:

Host name: 0c299cb8f897

Sample Solution-2:

Python Code:

import platform
host_name = platform.uname()[1]
print("Host name:", host_name )

Sample Output:

Host name: 4735090b6baa

Sample Solution-3:

Python Code:

import os
host_name = os.uname().nodename
print("Host name:", host_name)

Sample Output:

Host name: 0c299cb8f897

Python Code Editor:

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

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 Python skills with w3resource's quiz



Python: Tips of the Day

List comprehension:

>>> m = [x ** 2 for x in range(5)]
>>> m
[0, 1, 4, 9, 16]