How to get the hostname of the node in Python?

Posted on

In Python, how to get the hostname of the node?

In Python, you can get the hostname by the
socket.gethostname()
library function in the socket module:

import socket

hostname = socket.gethostname()

Example:

$ python
Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> socket.gethostname()
'host001'

Leave a Reply

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