IPv6 name resolution using getaddrinfo

O.R.Senthil Kumaran orsenthil at gmail.com
Sun Sep 16 12:05:23 CEST 2007


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:

>>> for res in socket.getaddrinfo('goofy.goofy.com', None,
>>> socket.AI_CANONNAME):
	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?

-- 
O.R.Senthil Kumaran
http://uthcode.sarovar.org



More information about the Python-list mailing list