Gethostbyname_ex() Function

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:

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:

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

 


Copyright 2023 © pythontic.com