1.. _example_reverse_lookup:
2
3Reverse DNS lookup
4==================
5
6Reverse DNS lookup involves determining the hostname associated with a given IP
7address.
8This example shows how reverse lookup can be done using unbound module.
9
10For the reverse DNS records, the special domain in-addr.arpa is reserved.
11For example, a host name for the IP address ``74.125.43.147`` can be obtained
12by issuing a DNS query for the PTR record for address
13``147.43.125.74.in-addr.arpa.``
14
15Source code
16-----------
17
18::
19
20    #!/usr/bin/python
21    import unbound
22
23    ctx = unbound.ub_ctx()
24    ctx.resolvconf("/etc/resolv.conf")
25
26    status, result = ctx.resolve(unbound.reverse("74.125.43.147") + ".in-addr.arpa.", unbound.RR_TYPE_PTR, unbound.RR_CLASS_IN)
27    if status == 0 and result.havedata:
28        print "Result.data:", result.data.domain_list
29    elif status != 0:
30        print "Resolve error:", unbound.ub_strerror(status)
31
32In order to simplify the python code, unbound module contains the
33:meth:`unbound.reverse` function which reverses the hostname components.
34This function is defined as follows::
35
36    def reverse(domain):
37        return '.'.join([a for a in domain.split(".")][::-1])
38