17
17
import socket
18
18
import time
19
19
import sys
20
+ import ssl
20
21
21
22
try :
22
23
from threading import Lock
@@ -53,7 +54,7 @@ class NagleMixin:
53
54
54
55
Based on code originally copied from Python 2.7's httplib module.
55
56
"""
56
-
57
+
57
58
def endheaders (self , message_body = None ):
58
59
if self .__dict__ ['_HTTPConnection__state' ] == _CS_REQ_STARTED :
59
60
self .__dict__ ['_HTTPConnection__state' ] = _CS_REQ_SENT
@@ -216,10 +217,25 @@ def __init__(self, cache=None, timeout=None, max_redirects=5,
216
217
self .cache = cache
217
218
self .max_redirects = max_redirects
218
219
self .perm_redirects = {}
219
- self .connection_pool = ConnectionPool (timeout )
220
+ self ._disable_ssl_verification = False
221
+ self ._timeout = timeout
222
+ self .connection_pool = ConnectionPool (
223
+ self ._timeout ,
224
+ disable_ssl_verification = self ._disable_ssl_verification )
220
225
self .retry_delays = list (retry_delays ) # We don't want this changing on us.
221
226
self .retryable_errors = set (retryable_errors )
222
227
228
+ def disable_ssl_verification (self ):
229
+ """
230
+ Disable verification of SSL certificates and re-initialize the ConnectionPool.
231
+ Only applicable on Python 2.7.9+ as previous versions of Python don't verify
232
+ SSL certs.
233
+
234
+ :return:
235
+ """
236
+ self ._disable_ssl_verification = True
237
+ self .connection_pool = ConnectionPool (self ._timeout , disable_ssl_verification = self ._disable_ssl_verification )
238
+
223
239
def request (self , method , url , body = None , headers = None , credentials = None ,
224
240
num_redirects = 0 ):
225
241
if url in self .perm_redirects :
@@ -420,11 +436,23 @@ def _clean(self):
420
436
self .by_url = dict (ls [- self .keep_size :])
421
437
422
438
439
+ class InsecureHTTPSConnection (HTTPSConnection ):
440
+ """ Wrapper class to create an HTTPSConnection without SSl verification (the default behavior in
441
+ Python < 2.7.9).
442
+
443
+ See: https://docs.python.org/2/library/httplib.html#httplib.HTTPSConnection
444
+ """
445
+ def __init__ (self , * a , ** k ):
446
+ k ['context' ] = ssl ._create_unverified_context ()
447
+ HTTPSConnection .__init__ (self , * a , ** k )
448
+
449
+
423
450
class ConnectionPool (object ):
424
451
"""HTTP connection pool."""
425
452
426
- def __init__ (self , timeout ):
453
+ def __init__ (self , timeout , disable_ssl_verification = False ):
427
454
self .timeout = timeout
455
+ self .disable_ssl_verification = disable_ssl_verification
428
456
self .conns = {} # HTTP connections keyed by (scheme, host)
429
457
self .lock = Lock ()
430
458
@@ -448,7 +476,10 @@ def get(self, url):
448
476
if scheme == 'http' :
449
477
cls = HTTPConnection
450
478
elif scheme == 'https' :
451
- cls = HTTPSConnection
479
+ if self .disable_ssl_verification :
480
+ cls = InsecureHTTPSConnection
481
+ else :
482
+ cls = HTTPSConnection
452
483
else :
453
484
raise ValueError ('%s is not a supported scheme' % scheme )
454
485
conn = cls (host , timeout = self .timeout )
0 commit comments