Gethostname() Function In Python

Function Name:



Function Signature:




  • The Python function socket.gethostname() returns the host name of the current system under which the Python interpreter is executed.




Return Value:

Host name of the localhost is returned.


Python Example using gethostname():

# sample python program for gethostname

import socket


# Get the local host name

myHostName = socket.gethostname()

print("Name of the localhost is {}".format(myHostName))


# Get the IP address of the local host

myIP = socket.gethostbyname(myHostName)

print("IP address of the localhost is {}".format(myIP))



Output of Python Example using gethostname:

Name of the localhost is mysystem.local

IP address of the localhost is


Copyright 2023 ©