| 1 |
import asyncio |
| 2 |
import threading |
| 3 |
|
| 4 |
import dns.asyncbackend |
| 5 |
import dns.asyncquery |
| 6 |
import dns.message |
| 7 |
import dns.query |
| 8 |
import dns.quic |
| 9 |
import dns.rdatatype |
| 10 |
|
| 11 |
try: |
| 12 |
import trio |
| 13 |
|
| 14 |
have_trio = True |
| 15 |
except ImportError: |
| 16 |
have_trio = False |
| 17 |
|
| 18 |
# This demo assumes you have the aioquic example doq_server.py running on localhost |
| 19 |
# on port 4784 on localhost. |
| 20 |
peer_address = "127.0.0.1" |
| 21 |
peer_port = 4784 |
| 22 |
query_name = "www.dnspython.org" |
| 23 |
tls_verify_mode = False |
| 24 |
|
| 25 |
|
| 26 |
def squery(rdtype="A", connection=None): |
| 27 |
q = dns.message.make_query(query_name, rdtype) |
| 28 |
r = dns.query.quic( |
| 29 |
q, peer_address, port=peer_port, connection=connection, verify=tls_verify_mode |
| 30 |
) |
| 31 |
print(r) |
| 32 |
|
| 33 |
|
| 34 |
def srun(): |
| 35 |
squery() |
| 36 |
|
| 37 |
|
| 38 |
def smultirun(): |
| 39 |
with dns.quic.SyncQuicManager(verify_mode=tls_verify_mode) as manager: |
| 40 |
connection = manager.connect(peer_address, peer_port) |
| 41 |
t1 = threading.Thread(target=squery, args=["A", connection]) |
| 42 |
t1.start() |
| 43 |
t2 = threading.Thread(target=squery, args=["AAAA", connection]) |
| 44 |
t2.start() |
| 45 |
t1.join() |
| 46 |
t2.join() |
| 47 |
|
| 48 |
|
| 49 |
async def aquery(rdtype="A", connection=None): |
| 50 |
q = dns.message.make_query(query_name, rdtype) |
| 51 |
r = await dns.asyncquery.quic( |
| 52 |
q, peer_address, port=peer_port, connection=connection, verify=tls_verify_mode |
| 53 |
) |
| 54 |
print(r) |
| 55 |
|
| 56 |
|
| 57 |
def arun(): |
| 58 |
asyncio.run(aquery()) |
| 59 |
|
| 60 |
|
| 61 |
async def amulti(): |
| 62 |
async with dns.quic.AsyncioQuicManager(verify_mode=tls_verify_mode) as manager: |
| 63 |
connection = manager.connect(peer_address, peer_port) |
| 64 |
t1 = asyncio.Task(aquery("A", connection)) |
| 65 |
t2 = asyncio.Task(aquery("AAAA", connection)) |
| 66 |
await t1 |
| 67 |
await t2 |
| 68 |
|
| 69 |
|
| 70 |
def amultirun(): |
| 71 |
asyncio.run(amulti()) |
| 72 |
|
| 73 |
|
| 74 |
if have_trio: |
| 75 |
|
| 76 |
def trun(): |
| 77 |
trio.run(aquery) |
| 78 |
|
| 79 |
async def tmulti(): |
| 80 |
async with trio.open_nursery() as nursery: |
| 81 |
async with dns.quic.TrioQuicManager( |
| 82 |
nursery, verify_mode=tls_verify_mode |
| 83 |
) as manager: |
| 84 |
async with trio.open_nursery() as query_nursery: |
| 85 |
# We run queries in a separate nursery so we can demonstrate |
| 86 |
# waiting for them all to exit without waiting for the manager to |
| 87 |
# exit as well. |
| 88 |
connection = manager.connect(peer_address, peer_port) |
| 89 |
query_nursery.start_soon(aquery, "A", connection) |
| 90 |
query_nursery.start_soon(aquery, "AAAA", connection) |
| 91 |
|
| 92 |
def tmultirun(): |
| 93 |
trio.run(tmulti) |
| 94 |
|
| 95 |
|
| 96 |
def main(): |
| 97 |
print("*** Single Queries ***") |
| 98 |
print("--- Sync ---") |
| 99 |
srun() |
| 100 |
print("--- Asyncio ---") |
| 101 |
dns.asyncbackend.set_default_backend("asyncio") |
| 102 |
arun() |
| 103 |
if have_trio: |
| 104 |
print("--- Trio ---") |
| 105 |
dns.asyncbackend.set_default_backend("trio") |
| 106 |
trun() |
| 107 |
print("*** Multi-connection Queries ***") |
| 108 |
print("--- Sync ---") |
| 109 |
smultirun() |
| 110 |
print("--- Asyncio ---") |
| 111 |
dns.asyncbackend.set_default_backend("asyncio") |
| 112 |
amultirun() |
| 113 |
if have_trio: |
| 114 |
print("--- Trio ---") |
| 115 |
dns.asyncbackend.set_default_backend("trio") |
| 116 |
tmultirun() |
| 117 |
|
| 118 |
|
| 119 |
if __name__ == "__main__": |
| 120 |
main() |