Created
April 23, 2012 14:57
-
-
Save michaelhelmick/2471457 to your computer and use it in GitHub Desktop.
Fix SSL Error in urllib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import httplib | |
import ssl | |
import socket | |
import sys | |
import urllib2 | |
class HTTPSConnectionV3(httplib.HTTPSConnection): | |
def __init__(self, *args, **kwargs): | |
httplib.HTTPSConnection.__init__(self, *args, **kwargs) | |
def connect(self): | |
sock = socket.create_connection((self.host, self.port), self.timeout) | |
if sys.version_info < (2, 6, 7): | |
if hasattr(self, '_tunnel_host'): | |
self.sock = sock | |
self._tunnel() | |
else: | |
if self._tunnel_host: | |
self.sock = sock | |
self._tunnel() | |
try: | |
self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file, ssl_version=ssl.PROTOCOL_SSLv3) | |
except ssl.SSLError: | |
self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file, ssl_version=ssl.PROTOCOL_SSLv23) | |
class HTTPSHandlerV3(urllib2.HTTPSHandler): | |
def https_open(self, req): | |
return self.do_open(HTTPSConnectionV3, req) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
urllib2.install_opener(urllib2.build_opener(HTTPSHandlerV3())) | |
urllib2.urlopen("https://yoursecureurl.com/") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this shows that requests is better than urllib2 ?