title: SSL_CERTIFICATE_FAILED error + corporate proxy urrlib2
body: <p>I have having trouble accessing <code>HTTPS</code> sites using <code>urllib2</code>. Here's what I've got:</p>
  
  <pre><code>import urllib2 
  import ssl
  
  proxy = urllib2.ProxyHandler({'https':'http://username:password@proxy:port',
                                'http': 'http://username:password@proxy:port'})
  opener = urllib2.build_opener(urllib2.HTTPHandler(), 
                                urllib2.HTTPSHandler(), 
                                proxy)
  urllib2.install(opener)
  
  url_secure = 'https://www.google.com'
  url_nonsecure = 'http://www.google.com'
  
  response = urllib2.urlopen(url_nonsecure)
  d = response.read()
  response.close()
  print d
  </code></pre>
  
  <p>The above runs without issue. However, if i try to run the above using </p>
  
  <pre><code>response = urllib2.urlopen(url_secure)
  </code></pre>
  
  <p>I get</p>
  
  <pre><code>Traceback (most recent call last):
  File "google_maps.py", line 25, in &lt;module&gt;
  response = urllib2.urlopen(url_secure)
  File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 154, in urlopen
  return opener.open(url, data, timeout)
  File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 429, in open
  response = self._open(req, data)
  File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 447, in _open
  '_open', req)
  File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 407, in _call_chain
  result = func(*args)
  File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 1241, in https_open
  context=self._context)
  File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 1198, in do_open
  raise URLError(err)
  urllib2.URLError: &lt;urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)&gt;
  </code></pre>
  
  <p>From <a href="https://stackoverflow.com/questions/19268548/python-ignore-certicate-validation-urllib2">this</a> and <a href="https://stackoverflow.com/questions/27835619/ssl-certificate-verify-failed-error">this</a> it seemed like the problem could be solved by creating a new context. So I tried both:</p>
  
  <pre><code>ctx=ssl._create_unverified_context()
  </code></pre>
  
  <p>and </p>
  
  <pre><code>ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
  </code></pre>
  
  <p>and ran</p>
  
  <pre><code>response = urllib2.urlopen(url_secure, context = ctx)
  d = response.read()
  response.close()
  print d
  </code></pre>
  
  <p>which both threw the following error:</p>
  
  <pre><code>Traceback (most recent call last):
  File "google_maps.py", line 25, in &lt;module&gt;
  response = urllib2.urlopen(url_secure, context=ctx)
  File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 154, in urlopen
  return opener.open(url, data, timeout)
  File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 429, in open
  response = self._open(req, data)
  File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 447, in _open
  '_open', req)
  File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 407, in _call_chain
  result = func(*args)
  File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 1241, in https_open
  context=self._context)
  File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 1198, in do_open
  raise URLError(err)
  urllib2.URLError: &lt;urlopen error [Errno 8] nodename nor servname provided, or not known&gt;
  </code></pre>
  
  <p>How can I access https sites?</p>