title: Python - How to handle HTTPS request with (Urllib2 + SSL) though a HTTP proxy
body: <p>I am trying to test a proxy connection by using urllib2.ProxyHandler. However, there probably some situation that I am going to request a HTTPS website (eg: <a href="https://www.whatismyip.com/" rel="nofollow noreferrer">https://www.whatismyip.com/</a>)</p>
  
  <p>Urllib2.urlopen() will throw ERROR if request a HTTPS site. So I tried to use a helper function to rewrite the URLOPEN method.</p>
  
  <p>Here is the helper function:</p>
  
  <pre><code>def urlopen(url, timeout):
      if hasattr(ssl, 'SSLContext'):
          SslContext = ssl.create_default_context()
          SslContext.check_hostname = False
          SslContext.verify_mode = ssl.CERT_NONE
          return urllib2.urlopen(url, timeout=timeout, context=SslContext)
      else:
          return urllib2.urlopen(url, timeout=timeout)
  </code></pre>
  
  <p>This helper function based on <a href="https://stackoverflow.com/questions/19268548/python-ignore-certicate-validation-urllib2">answer</a></p>
  
  <p>Then I use:</p>
  
  <pre><code>urllib2.install_opener(
       urllib2.build_opener(
           urllib2.ProxyHandler({'http': '127.0.0.1:8080'})
       )
  )
  </code></pre>
  
  <p>to setup http proxy for urllib.opener. </p>
  
  <p>Ideally, it should working when i request a website by using <code>urlopen('http://whatismyip.com', 30)</code> and it should pass all traffic through http proxy.</p>
  
  <p>However, the <code>urlopen()</code> will fall into <code>if hasattr(ssl, 'SSLContext')</code> all the time even if it is a HTTP site. In addition, HTTPS site is not using HTTP proxy either. This cause the HTTP proxy become invalid and all traffic going through unproxied network</p>
  
  <p>I also tried this <a href="https://stackoverflow.com/a/3248895/4196468">answer</a> to change HTTP into HTTPS <code>urllib2.ProxyHandler({'https': '127.0.0.1:8080'})</code> but it still not working.</p>
  
  <p>My proxy is working. If i am using <code>urllib2.urlopen()</code> instead of the rewrite version <code>urlopen()</code>, it works for HTTP site.</p>
  
  <p>But, I do need consider the suitation if the <code>urlopen</code> gonna need to be used on a HTTPS ONLY site.</p>
  
  <p>How to do that?</p>
  
  <p>Thanks</p>
  
  <p><strong>UPDATE1:</strong> I cannot get this work with Python 2.7.11 and some of server working properly with Python 2.7.5. I assue it is python version issue.</p>
  
  <p>Urllib2 will not go through HTTPS Proxy so all HTTPS web address will failed to use proxy.</p>