From cc2739fc0f31e9e9f0ae05a8908af2f19d69c751 Mon Sep 17 00:00:00 2001 From: Jason Antman Date: Wed, 15 Apr 2015 08:06:50 -0400 Subject: [PATCH] gist.py - handle older pythons where we can't disable ssl verification --- gist.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/gist.py b/gist.py index a00607e..aed353f 100755 --- a/gist.py +++ b/gist.py @@ -20,6 +20,9 @@ 2015-02-05 jantman: - initial script + +2015-04-15 jantman: +- catch error on ssl import and disable no_verify option """ import httplib @@ -32,12 +35,18 @@ import json import logging from copy import deepcopy -from ssl import _create_unverified_context FORMAT = "[%(levelname)s %(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s" logging.basicConfig(level=logging.ERROR, format=FORMAT) logger = logging.getLogger(__name__) +try: + from ssl import _create_unverified_context + have_ssl = True +except ImportError: + logger.error("ERROR - could not import ssl._create_unverified_context; unable to disable SSL cert verification") + have_ssl = False + def debug_response(response): logger.debug("Response status {s}".format(s=response.status)) logger.debug("Response: {d}".format(d=response.read())) @@ -115,6 +124,10 @@ def gist_write(name, content, token=None, prefix=False, no_verify=False): if options.verbose: logger.setLevel(logging.DEBUG) +if options.no_verify and not have_ssl: + logger.error("ERROR: could not import ssl._create_unverified_context; therefore unable to disable SSL cert verification") + raise SystemExit(1) + if len(args) < 1: sys.stderr.write(usage + "\n") raise SystemExit(1)