Skip to content

Commit

Permalink
Ignoring SSL checks for image download
Browse files Browse the repository at this point in the history
  • Loading branch information
mtlynch committed May 30, 2018
1 parent 7287118 commit 97e683d
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion downloader/url_downloader.py
@@ -1,3 +1,4 @@
import ssl
import urllib2
import urlparse

Expand All @@ -17,13 +18,26 @@ class UnexpectedImageType(Error):
def download_image_data(image_url):
image_handle = urllib2.urlopen(
urllib2.Request(
_encode_url(image_url), headers={'User-Agent': _USER_AGENT}))
_encode_url(image_url), headers={'User-Agent': _USER_AGENT}),
context=_get_insecure_ssl_context())
if image_handle.info().type not in ['image/jpeg', 'image/png']:
raise UnexpectedImageType('Unexpected image type: ' +
image_handle.info().type)
return image_handle.read()


def _get_insecure_ssl_context():
"""Creates a no-verify SSL context.
Since we're just downloading images, we'd rather blindly accept bad
certificates than fail when a site has bad SSL configurations.
"""
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
return context


def _encode_url(url):
scheme, network_location, path, query, fragment_identifier = urlparse.urlsplit(
url)
Expand Down

0 comments on commit 97e683d

Please sign in to comment.