Skip to content

Commit e188799

Browse files
authored
Don't create default SSLContext if ssl module isn't present (#6724)
1 parent 145b539 commit e188799

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/requests/adapters.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,18 @@ def SOCKSProxyManager(*args, **kwargs):
7373
DEFAULT_RETRIES = 0
7474
DEFAULT_POOL_TIMEOUT = None
7575

76-
_preloaded_ssl_context = create_urllib3_context()
77-
_preloaded_ssl_context.load_verify_locations(
78-
extract_zipped_paths(DEFAULT_CA_BUNDLE_PATH)
79-
)
76+
77+
try:
78+
import ssl # noqa: F401
79+
80+
_preloaded_ssl_context = create_urllib3_context()
81+
_preloaded_ssl_context.load_verify_locations(
82+
extract_zipped_paths(DEFAULT_CA_BUNDLE_PATH)
83+
)
84+
except ImportError:
85+
# Bypass default SSLContext creation when Python
86+
# interpreter isn't built with the ssl module.
87+
_preloaded_ssl_context = None
8088

8189

8290
def _urllib3_request_context(
@@ -90,13 +98,19 @@ def _urllib3_request_context(
9098
parsed_request_url = urlparse(request.url)
9199
scheme = parsed_request_url.scheme.lower()
92100
port = parsed_request_url.port
101+
102+
# Determine if we have and should use our default SSLContext
103+
# to optimize performance on standard requests.
93104
poolmanager_kwargs = getattr(poolmanager, "connection_pool_kw", {})
94105
has_poolmanager_ssl_context = poolmanager_kwargs.get("ssl_context")
106+
should_use_default_ssl_context = (
107+
_preloaded_ssl_context is not None and not has_poolmanager_ssl_context
108+
)
95109

96110
cert_reqs = "CERT_REQUIRED"
97111
if verify is False:
98112
cert_reqs = "CERT_NONE"
99-
elif verify is True and not has_poolmanager_ssl_context:
113+
elif verify is True and should_use_default_ssl_context:
100114
pool_kwargs["ssl_context"] = _preloaded_ssl_context
101115
elif isinstance(verify, str):
102116
if not os.path.isdir(verify):

0 commit comments

Comments
 (0)