Gethostbyname() Function In Python

Function Name:



Function Signature:




  • Given a host name the gethostbyname() function returns the IP address of the host.


  • Unlike, gethostbyname() returns just one ip of the host even though the host  could resolve to multiple IPs from a given location.


  • The returned IP address is an IPv4 address.  


  • If the developer needs to resolve the hostname into IPv6 addresses or both IPv4 and IPv6 addresses are needed, socket.getaddrinfo() function can be used.


  • When the parameter hostname is omitted hostname defaults to localhost.



hostname - The name of the host system for which IP address resolution is needed.


Return Value:

The IPv4 address of the host name provided.



import socket


hostName    = ""


ipAddress   = socket.gethostbyname(hostName)

print("IP address of the host name {} is: {}".format(hostName, ipAddress))



IP address of the host name is:


Copyright 2024 ©