gethostbyname_ex() function

The gethostbyname_ex() function in Python takes a host name and returns a tuple containing host name, aliases and other ip addresses of the host

socket Updated 2 Feb 2017 1 min read

Function Name

gethostbyname_ex

Function Signature

gethostbyname_ex(hostname)

Overview

Given a host name the gethostbyname_ex() function returns a tuple containing following information:

  • Host name
  • Alias names of the host name
  • Other IP addresses of the host name

If the developer wants to get just one IP address corresponding to the host name,

the socket.gethostbyname(hostname) function can be used.

The gethostbyname_ex() method returns only the IPv4 addresses.

For resolving IPv6 addresses as well socket.getaddrinfo() function can be used.

Parameters

hostname - The name of the host system for which the hostname, aliases, IP addresses list is needed

Return Value

A tuple containing the hostname, aliases and list of IP addresses.

Example

python
import socket

hostName    = "example.org"

ipAddress   = socket.gethostbyname_ex(hostName)

print("Primary IP address, Alias names and other IP addresses of the host name {}: {}".format(hostName, ipAddress))

Output

Output
Primary IP address, Alias names and other IP addresses of the host name example.org: ('example.org', [], ['93.184.216.34'])