summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorBob Halley <halley@dnspython.org>2023-03-19 09:24:32 -0700
committerGitHub <noreply@github.com>2023-03-19 09:24:32 -0700
commit6c5f0c9d8086c999357531c38b831efd24b6b5ac (patch)
tree2492dd98f3bda103b07e99e4ed834cad3d0a2dbd /examples
parent1aeec724042b0af114ebf3f9966e7ea4be1dad33 (diff)
downloaddnspython-6c5f0c9d8086c999357531c38b831efd24b6b5ac.tar.gz
Better DNS-over-HTTPS support. (#908)
This change: Allows resolution hostnames in URLs using dnspython's resolver or via a bootstrap address, without rewriting URLs. Adds full support for source addresses and ports to httpx, except for asyncio I/O where only the source address can be specified. Removes support for requests.
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/doh-json.py4
-rwxr-xr-xexamples/doh.py27
2 files changed, 6 insertions, 25 deletions
diff --git a/examples/doh-json.py b/examples/doh-json.py
index e9fa087..c8d830b 100755
--- a/examples/doh-json.py
+++ b/examples/doh-json.py
@@ -2,7 +2,7 @@
import copy
import json
-import requests
+import httpx
import dns.flags
import dns.message
@@ -92,7 +92,7 @@ def from_doh_simple(simple, add_qr=False):
a = dns.resolver.resolve("www.dnspython.org", "a")
p = to_doh_simple(a.response)
print(json.dumps(p, indent=4))
-response = requests.get(
+response = httpx.get(
"https://dns.google/resolve?",
verify=True,
params={"name": "www.dnspython.org", "type": 1},
diff --git a/examples/doh.py b/examples/doh.py
index 17787ed..2fd44ff 100755
--- a/examples/doh.py
+++ b/examples/doh.py
@@ -1,11 +1,7 @@
#!/usr/bin/env python3
#
# This is an example of sending DNS queries over HTTPS (DoH) with dnspython.
-# Requires use of the requests module's Session object.
-#
-# See https://2.python-requests.org/en/latest/user/advanced/#session-objects
-# for more details about Session objects
-import requests
+import httpx
import dns.message
import dns.query
@@ -13,31 +9,16 @@ import dns.rdatatype
def main():
- where = "1.1.1.1"
+ where = "https://dns.google/dns-query"
qname = "example.com."
- # one method is to use context manager, session will automatically close
- with requests.sessions.Session() as session:
+ with httpx.Client() as client:
q = dns.message.make_query(qname, dns.rdatatype.A)
- r = dns.query.https(q, where, session=session)
+ r = dns.query.https(q, where, session=client)
for answer in r.answer:
print(answer)
# ... do more lookups
- where = "https://dns.google/dns-query"
- qname = "example.net."
- # second method, close session manually
- session = requests.sessions.Session()
- q = dns.message.make_query(qname, dns.rdatatype.A)
- r = dns.query.https(q, where, session=session)
- for answer in r.answer:
- print(answer)
-
- # ... do more lookups
-
- # close the session when you're done
- session.close()
-
if __name__ == "__main__":
main()