urllib not taking context as a parameter
Question: I'm trying to add the sssl.SSlContext to a urlopen method but keep getting the
error:

    
    
    TypeError: urlopen() got an unexpected keyword argument 'context'
    

I'm using python 3 and urllib. This has a context parameter defined -
<https://docs.python.org/2/library/urllib.html>. So I don't understand why it
is throwing the error. But either way this is the code:

    
    
    try:
        # For Python 3.0 and later
        from urllib.request import urlopen, Request
    except ImportError:
        # Fall back to Python 2's urllib2
        from urllib2 import urlopen, Request
    request = Request(url, content, headers)
    request.get_method = lambda: method
    
    if sys.version_info[0] == 2 and sys.version_info[1] < 8:
        result = urlopen(request)
    else:
        gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
        result = urlopen(request, context=gcontext)
    

Can someone explain what I am doing wrong?


Answer: According to [`urllib.request.urlopen`
documentation](https://docs.python.org/3/library/urllib.request.html#module-
urllib.request):

> Changed in version 3.4.3: context was added.

the parameter `context` will be added in Python 3.4.3. You need to fall back
for lower version.

* * *

In Python 2.x, it's added in Python 2.7.9.
([`urllib.urlopen`](https://docs.python.org/2/library/urllib.html#urllib.urlopen),
[`urllib2.urlopen`](https://docs.python.org/2/library/urllib2.html#urllib2.urlopen))

