IPv6 name resolution using getaddrinfo

Discussion in 'Python' started by O.R.Senthil Kumaran, Sep 16, 2007.

  1. 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
     
    O.R.Senthil Kumaran, Sep 16, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew FPGA
    Replies:
    0
    Views:
    1,012
    Andrew FPGA
    Sep 26, 2005
  2. Bernhard Schmidt

    result sorting in socket.getaddrinfo?

    Bernhard Schmidt, Oct 9, 2004, in forum: Python
    Replies:
    0
    Views:
    396
    Bernhard Schmidt
    Oct 9, 2004
  3. Nathan
    Replies:
    4
    Views:
    465
    Nathan
    Jun 18, 2008
  4. rocky
    Replies:
    2
    Views:
    335
    rocky
    Jul 7, 2009
  5. Jesse van den Kieboom

    Ipv6 on a ipv4/ipv6 hostname

    Jesse van den Kieboom, Jun 5, 2005, in forum: Ruby
    Replies:
    1
    Views:
    397
    Dibya Prakash
    Jun 5, 2005
Loading...

Share This Page