gethostbyaddr() function in Python

Given an IP address, the gethostbyaddr() function in Python returns a tuple with host name, aliases and the list of ip addresses for that host interface

socket Updated 3 Feb 2017 1 min read

Function Name

gethostbyaddr

Function Signature

gethostbyaddr(ip_address)

Function Overview

Given an IP address the function socket.gethostbyaddr() returns a tuple containing

  • Host Name
  • Alias list for the IP address if any
  • IP address of the host

Example

python
import socket

ipAddress = "192.0.43.8"

hostName = socket.gethostbyaddr(ipAddress)

print("Host Name for the IP address {} is {}".format(ipAddress, hostName))

Output

Output
Host Name for the IP address 192.0.43.8 is ('43-8.any.icann.org', ['8.43.0.192.in-addr.arpa'], ['192.0.43.8'])