xref: /netbsd-src/external/bsd/unbound/dist/libunbound/python/doc/examples/example1a.rst (revision 0cd9f4ecf44538bbdd5619b5b2081449960ab3e6)
13b6c3722Schristos.. _example_resolve_name:
23b6c3722Schristos
33b6c3722SchristosResolve a name
4*0cd9f4ecSchristos==============
53b6c3722Schristos
6*0cd9f4ecSchristosThis basic example shows how to create a context and resolve a host address
7*0cd9f4ecSchristos(DNS record of A type).
8*0cd9f4ecSchristos
9*0cd9f4ecSchristosSource code
10*0cd9f4ecSchristos-----------
113b6c3722Schristos
123b6c3722Schristos::
133b6c3722Schristos
143b6c3722Schristos    #!/usr/bin/python
153b6c3722Schristos    import unbound
163b6c3722Schristos
173b6c3722Schristos    ctx = unbound.ub_ctx()
183b6c3722Schristos    ctx.resolvconf("/etc/resolv.conf")
193b6c3722Schristos
203b6c3722Schristos    status, result = ctx.resolve("www.google.com")
213b6c3722Schristos    if status == 0 and result.havedata:
223b6c3722Schristos        print "Result.data:", result.data.address_list
233b6c3722Schristos    elif status != 0:
243b6c3722Schristos        print "Resolve error:", unbound.ub_strerror(status)
253b6c3722Schristos
26*0cd9f4ecSchristosIn contrast with the C API, the source code is more compact while the
27*0cd9f4ecSchristosperformance of C implementation is preserved.
28*0cd9f4ecSchristosThe main advantage is that you need not take care about the deallocation and
29*0cd9f4ecSchristosallocation of context and result structures; pyUnbound module does it
30*0cd9f4ecSchristosautomatically for you.
313b6c3722Schristos
32*0cd9f4ecSchristosIf only domain name is given, the :meth:`unbound.ub_ctx.resolve` looks for
33*0cd9f4ecSchristosA records in IN class.
34