IPv6 name resolution using getaddrinfo

  • Thread starter O.R.Senthil Kumaran
  • Start date
O

O.R.Senthil Kumaran

Hi all,
To get the hostname, I can use socket.gethostbyname() but that has an
inherent limitation wherein does it not support IPv6 name resolution, and
getaddrinfo() should be used instead.

Looking up the socket.getaddrinfo() documentation, I come to know that

The getaddrinfo() function returns a list of 5-tuples with the following
structure:

(family, socktype, proto, canonname, sockaddr)

family, socktype, proto are all integer and are meant to be passed to
the socket() function. canonname is a string representing the canonical
name of the host. It can be a numeric IPv4/v6 address when AI_CANONNAME
is specified for a numeric host.

With this information, if I try something like this:
print res


(2, 1, 6, '', ('10.98.1.6', 0))
(2, 2, 17, '', ('10.98.1.6', 0))
(2, 3, 0, '', ('10.98.1.6', 0))

In the output, I see the cannoname to be always blank ''. I am not
getting the IPv4 or IPv6 address as a result of using getaddrinfo().

Am I making any mistake?

What i am trying is a replacement function for
socket.gethostbyname(hostname) which will work for both IPv4 and IPv6.

# return hostbyname for either IPv4 or IPv6 address. Common function.

def ipv6_gethostbyname(hostname):
for res in socket.getaddrinfo(hostname,None,
socket.AI_CANONNAME):
fa, socktype, proto, canonname, sa = res
return cannoname

The above function does not seem to work. It returns blank value only.

Any help/ pointers?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top