summaryrefslogtreecommitdiff
path: root/dns/query.py
diff options
context:
space:
mode:
authorDaniel Lenski <dlenski@amazon.com>2020-04-30 17:31:28 -0700
committerDaniel Lenski <dlenski@amazon.com>2020-04-30 17:35:27 -0700
commitea9b29fb1d1b7d764561621261381f544fa6fd41 (patch)
tree2846e9f7ffc51aa735e0ec30a533de4835f9c341 /dns/query.py
parentf6d22293e09fbcea870ee2fbd6dcfdf7eeda5068 (diff)
downloaddnspython-ea9b29fb1d1b7d764561621261381f544fa6fd41.tar.gz
make DOH an extra feature (to avoid requests as hard dependency)
Diffstat (limited to 'dns/query.py')
-rw-r--r--dns/query.py21
1 files changed, 18 insertions, 3 deletions
diff --git a/dns/query.py b/dns/query.py
index 725eff1..174a7fb 100644
--- a/dns/query.py
+++ b/dns/query.py
@@ -38,9 +38,14 @@ import dns.rcode
import dns.rdataclass
import dns.rdatatype
-import requests
-from requests_toolbelt.adapters.source import SourceAddressAdapter
-from requests_toolbelt.adapters.host_header_ssl import HostHeaderSSLAdapter
+try:
+ import requests
+ from requests_toolbelt.adapters.source import SourceAddressAdapter
+ from requests_toolbelt.adapters.host_header_ssl import HostHeaderSSLAdapter
+ have_doh = True
+except ImportError:
+ have_doh = False
+
try:
import ssl
@@ -76,6 +81,11 @@ class TransferError(dns.exception.DNSException):
self.rcode = rcode
+class NoDOH(dns.exception.DNSException):
+ """DNS over HTTPS (DOH) was requested but the requests module is not
+ available."""
+
+
def _compute_expiration(timeout):
if timeout is None:
return None
@@ -226,6 +236,8 @@ def send_https(session, what, lifetime=None):
:param lifetime: timeout (in seconds)
:return: a :class:`requests.models.Response` object.
"""
+ if not have_doh:
+ raise NoDOH
if isinstance(what, requests.models.Request):
what = what.prepare()
return session.send(what, timeout=lifetime)
@@ -280,6 +292,9 @@ def https(q, where, timeout=None, port=443, af=None, source=None, source_port=0,
Returns a ``dns.message.Message``.
"""
+ if not have_doh:
+ raise NoDOH
+
wire = q.to_wire()
(af, destination, source) = _destination_and_source(af, where, port,
source, source_port,