[Python-Dev] IPv6 hostname resolution using socket.getaddrinfo()

O.R.Senthil Kumaran orsenthil at gmail.com
Wed Sep 19 03:27:40 CEST 2007


* Blinston_Fernandes at Dell.com <Blinston_Fernandes at Dell.com> :

> On python2.4.1
> 
> >>> socket.getaddrinfo('www.python.org', None, socket.AF_INET,
> socket.SOCK_DGRAM, socket.IPPROTO_IP, socket.AI_CANONNAME)
> [(2, 2, 17, 'dinsdale.python.org', ('82.94.237.218', 0))]
> >>>
> 
> Blinston.


Thanks a lot, Blinston. That helped.
I just have to take care of socket.AF_INET6 flag for IPv6 now.

>>>socket.getaddrinfo('localhost', None, socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_IP, socket.AI_CANONNAME)
[(10, 2, 17, 'localhost', ('fe80::219:5bff:fefd:6270', 0, 0, 0))]

Shall do a little more research on flags and see if documentation needs any update. Because current one speaks only about AI_CANONNAME being set.


Thank you. :)
Senthil



> 
> -----Original Message-----
> From: python-dev-bounces+blinston_fernandes=dell.com at python.org
> [mailto:python-dev-bounces+blinston_fernandes=dell.com at python.org] On
> Behalf Of O.R.Senthil Kumaran
> Sent: Monday, September 17, 2007 10:08 PM
> To: python-dev at python.org
> Subject: [Python-Dev] IPv6 hostname resolution using
> socket.getaddrinfo()
> 
> 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
> (and make changes in urllib2 to support that)
> 
> # 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
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/blinston_fernandes%40d
> ell.com

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


More information about the Python-Dev mailing list