xref: /netbsd-src/external/bsd/unbound/dist/libunbound/python/examples/idn-lookup.py (revision 3b6c3722d8f990f9a667d638078aee8ccdc3c0f3)
1*3b6c3722Schristos#!/usr/bin/python
2*3b6c3722Schristos# vim:fileencoding=utf-8
3*3b6c3722Schristos'''
4*3b6c3722Schristos idn-lookup.py: IDN (Internationalized Domain Name) lookup support
5*3b6c3722Schristos
6*3b6c3722Schristos Authors: Zdenek Vasicek (vasicek AT fit.vutbr.cz)
7*3b6c3722Schristos          Marek Vavrusa  (xvavru00 AT stud.fit.vutbr.cz)
8*3b6c3722Schristos
9*3b6c3722Schristos Copyright (c) 2008. All rights reserved.
10*3b6c3722Schristos
11*3b6c3722Schristos This software is open source.
12*3b6c3722Schristos
13*3b6c3722Schristos Redistribution and use in source and binary forms, with or without
14*3b6c3722Schristos modification, are permitted provided that the following conditions
15*3b6c3722Schristos are met:
16*3b6c3722Schristos
17*3b6c3722Schristos Redistributions of source code must retain the above copyright notice,
18*3b6c3722Schristos this list of conditions and the following disclaimer.
19*3b6c3722Schristos
20*3b6c3722Schristos Redistributions in binary form must reproduce the above copyright notice,
21*3b6c3722Schristos this list of conditions and the following disclaimer in the documentation
22*3b6c3722Schristos and/or other materials provided with the distribution.
23*3b6c3722Schristos
24*3b6c3722Schristos THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25*3b6c3722Schristos "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26*3b6c3722Schristos TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27*3b6c3722Schristos PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
28*3b6c3722Schristos LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29*3b6c3722Schristos CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30*3b6c3722Schristos SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31*3b6c3722Schristos INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32*3b6c3722Schristos CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33*3b6c3722Schristos ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34*3b6c3722Schristos POSSIBILITY OF SUCH DAMAGE.
35*3b6c3722Schristos'''
36*3b6c3722Schristosfrom __future__ import print_function
37*3b6c3722Schristosimport unbound
38*3b6c3722Schristosimport locale
39*3b6c3722Schristos
40*3b6c3722Schristosctx = unbound.ub_ctx()
41*3b6c3722Schristosctx.set_option("module-config:","iterator") #We don't need validation
42*3b6c3722Schristosctx.resolvconf("/etc/resolv.conf")
43*3b6c3722Schristos
44*3b6c3722Schristos#The unicode IDN string is automatically converted (if necessary)
45*3b6c3722Schristosstatus, result = ctx.resolve(u"www.háčkyčárky.cz", unbound.RR_TYPE_A, unbound.RR_CLASS_IN)
46*3b6c3722Schristosif status == 0 and result.havedata:
47*3b6c3722Schristos    print("Result:")
48*3b6c3722Schristos    print("      raw data:", result.data)
49*3b6c3722Schristos    for k in sorted(result.data.address_list):
50*3b6c3722Schristos        print("      address:%s" % k)
51*3b6c3722Schristos
52*3b6c3722Schristosstatus, result = ctx.resolve(u"háčkyčárky.cz", unbound.RR_TYPE_MX, unbound.RR_CLASS_IN)
53*3b6c3722Schristosif status == 0 and result.havedata:
54*3b6c3722Schristos    print("Result:")
55*3b6c3722Schristos    print("      raw data:", result.data)
56*3b6c3722Schristos    for k in sorted(result.data.mx_list_idn):
57*3b6c3722Schristos        print("      priority:%d address:%s" % k)
58*3b6c3722Schristos
59*3b6c3722Schristosstatus, result = ctx.resolve(unbound.reverse('217.31.204.66')+'.in-addr.arpa', unbound.RR_TYPE_PTR, unbound.RR_CLASS_IN)
60*3b6c3722Schristosif status == 0 and result.havedata:
61*3b6c3722Schristos    print("Result.data:", result.data)
62*3b6c3722Schristos    for k in sorted(result.data.domain_list_idn):
63*3b6c3722Schristos        print("      dname:%s" % k)
64