summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSenthil Kumaran <senthil@uthcode.com>2014-09-19 07:23:30 (GMT)
committerSenthil Kumaran <senthil@uthcode.com>2014-09-19 07:23:30 (GMT)
commita5c85b3f5fc12256680a93dda9d32138d47605de (patch)
tree8150c96aa51e8e20e0e54cf0603b7d9b9e9f9045 /Lib
parentea07eb9469d1c93a26560a00f6e4ca45d4c1ecdb (diff)
downloadcpython-a5c85b3f5fc12256680a93dda9d32138d47605de.zip
cpython-a5c85b3f5fc12256680a93dda9d32138d47605de.tar.gz
cpython-a5c85b3f5fc12256680a93dda9d32138d47605de.tar.bz2
Issue #22366: urllib.request.urlopen will accept a context object (SSLContext)
as an argument which will then used be for HTTPS connection. Patch by Alex Gaynor.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_urllib.py8
-rw-r--r--Lib/urllib/request.py10
2 files changed, 17 insertions, 1 deletions
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index 5478837..962ef73 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -10,6 +10,7 @@ import unittest
from unittest.mock import patch
from test import support
import os
+import ssl
import sys
import tempfile
from nturl2path import url2pathname, pathname2url
@@ -379,6 +380,13 @@ Content-Type: text/html; charset=iso-8859-1
with support.check_warnings(('',DeprecationWarning)):
urllib.request.URLopener()
+ def test_cafile_and_context(self):
+ context = ssl.create_default_context()
+ with self.assertRaises(ValueError):
+ urllib.request.urlopen(
+ "https://localhost", cafile="/nonexistent/path", context=context
+ )
+
class urlopen_DataTests(unittest.TestCase):
"""Test urlopen() opening a data URL."""
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
index 67c7566..e0c8116 100644
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -136,9 +136,14 @@ __version__ = sys.version[:3]
_opener = None
def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
- *, cafile=None, capath=None, cadefault=False):
+ *, cafile=None, capath=None, cadefault=False, context=None):
global _opener
if cafile or capath or cadefault:
+ if context is not None:
+ raise ValueError(
+ "You can't pass both context and any of cafile, capath, and "
+ "cadefault"
+ )
if not _have_ssl:
raise ValueError('SSL support not available')
context = ssl._create_stdlib_context(cert_reqs=ssl.CERT_REQUIRED,
@@ -146,6 +151,9 @@ def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
capath=capath)
https_handler = HTTPSHandler(context=context, check_hostname=True)
opener = build_opener(https_handler)
+ elif context:
+ https_handler = HTTPSHandler(context=context)
+ opener = build_opener(https_handler)
elif _opener is None:
_opener = opener = build_opener()
else: