views:

51

answers:

2

socket.getfqdn() works fine with IPv4 addresses, for example:

>>> import socket
>>> socket.getfqdn("8.8.8.8")
'google-public-dns-a.google.com'

However, it doesn't work for IPv6 addresses.

>>> socket.getfqdn("2404:6800:8004::68")
'2404:6800:8004::68'
>>> socket.has_ipv6
True

How can I do this with IPv6? Ideally with only modules in the standard library.

+3  A: 

Are you sure that ipv6 address has any revers DNS associated with it? dig reports it doesn't:

$ dig -x 2404:6800:8004::68

; <<>> DiG 9.4.3-P5 <<>> -x 2404:6800:8004::68
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 35573
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;8.6.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.4.0.0.8.0.0.8.6.4.0.4.2.ip6.arpa. IN PTR

;; Query time: 364 msec
;; SERVER: 12.165.58.2#53(12.165.58.2)
;; WHEN: Thu Sep  2 03:45:50 2010
;; MSG SIZE  rcvd: 90

edit: Finally found an ipv6 address that has reverse DNS associated. In short, works4me.

>>> import socket
>>> socket.has_ipv6
True
>>> socket.getfqdn('2001:838:2:1::30:67')
'gatey.sixxs.net'

And from dig:

$ dig -x 2001:838:2:1::30:67

; <<>> DiG 9.4.3-P5 <<>> -x 2001:838:2:1::30:67
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 934
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 13, ADDITIONAL: 1

;; QUESTION SECTION:
;7.6.0.0.0.3.0.0.0.0.0.0.0.0.0.0.1.0.0.0.2.0.0.0.8.3.8.0.1.0.0.2.ip6.arpa. IN PTR

;; ANSWER SECTION:
7.6.0.0.0.3.0.0.0.0.0.0.0.0.0.0.1.0.0.0.2.0.0.0.8.3.8.0.1.0.0.2.ip6.arpa. 43200 IN PTR gatey.sixxs.net.

;; AUTHORITY SECTION:
.                       517204  IN      NS      e.root-servers.net.
.                       517204  IN      NS      m.root-servers.net.
.                       517204  IN      NS      a.root-servers.net.
.                       517204  IN      NS      l.root-servers.net.
.                       517204  IN      NS      c.root-servers.net.
.                       517204  IN      NS      h.root-servers.net.
.                       517204  IN      NS      j.root-servers.net.
.                       517204  IN      NS      g.root-servers.net.
.                       517204  IN      NS      f.root-servers.net.
.                       517204  IN      NS      i.root-servers.net.
.                       517204  IN      NS      d.root-servers.net.
.                       517204  IN      NS      b.root-servers.net.
.                       517204  IN      NS      k.root-servers.net.

;; ADDITIONAL SECTION:
a.root-servers.net.     604222  IN      A       198.41.0.4

;; Query time: 383 msec
;; SERVER: 12.165.58.2#53(12.165.58.2)
;; WHEN: Thu Sep  2 03:55:03 2010
;; MSG SIZE  rcvd: 343
Aaron Gallagher
+1  A: 

Guess what: that address doesn’t have reverse DNS. For an easier-to-understand output than dig:

$ host 2404:6800:8004::68
Host 8.6.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.4.0.0.8.0.0.8.6.4.0.4.2.ip6.arpa not found: 3(NXDOMAIN)

Here’s an example of an address that does have reverse DNS:

$ host 2001:470:0:64::2
2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.4.6.0.0.0.0.0.0.0.7.4.0.1.0.0.2.ip6.arpa domain name pointer ipv6.he.net.

And hey presto, it works with your Python example:

>>> import socket
>>> socket.getfqdn("2001:470:0:64::2")
'ipv6.he.net'
Jeremy Visser