Debian Codegraph @

examples/ddr.py

raw
1 #!/usr/bin/env python3
2
3 # Using Discovery of Designated Resolvers (synchronous I/O)
4
5 import dns.resolver
6
7 res = dns.resolver.Resolver(configure=False)
8 res.nameservers = ["1.1.1.1"]
9 # Invoke try_ddr() to attempt to upgrade the connection via DDR
10 res.try_ddr()
11 # Do a sample resolution
12 for rr in res.resolve("www.google.com", "A"):
13 print(rr.address)
14 # Note that the nameservers have been upgraded
15 print(res.nameservers)
16
17
18 # Using Discovery of Designated Resolvers (asynchronous I/O)
19
20 # We show using asyncio, but if you comment out asyncio lines
21 # and uncomment the trio lines, it will work with trio too.
22
23 import asyncio
24
25 import dns.asyncresolver
26
27 # import trio
28
29
30 async def amain():
31 res = dns.asyncresolver.Resolver(configure=False)
32 res.nameservers = ["8.8.8.8"]
33 await res.try_ddr()
34
35 for rr in await res.resolve("www.google.com", "A"):
36 print(rr.address)
37
38 print(res.nameservers)
39
40
41 asyncio.run(amain())
42 # trio.run(amain)