How to get hostname in Python on Linux?

In Python, how to get hostname as the command hostname does on Linux?

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

import socket

hostname = socket.gethostname()


Leave a Reply

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