Added argument to HTTP client constructor to disable SSL certificate verification ZTK-1.1
authorThierry Florac <tflorac@ulthar.net>
Thu, 04 Jun 2020 15:48:20 +0200
branchZTK-1.1
changeset 274 0126d5a339de
parent 273 1045afa4242c
child 275 2027d75ae843
Added argument to HTTP client constructor to disable SSL certificate verification
src/ztfy/utils/protocol/http.py
--- a/src/ztfy/utils/protocol/http.py	Tue Nov 17 12:16:52 2015 +0100
+++ b/src/ztfy/utils/protocol/http.py	Thu Jun 04 15:48:20 2020 +0200
@@ -13,26 +13,18 @@
 #
 ##############################################################################
 
-
-# import standard packages
-import httplib2
 import urllib
 import urlparse
 
-# import Zope3 interfaces
-
-# import local interfaces
-
-# import Zope3 packages
-
-# import local packages
+import httplib2
 
 
 class HTTPClient(object):
     """HTTP client"""
 
     def __init__(self, method, protocol, servername, url, params={}, credentials=(),
-                 proxy=(), rdns=True, proxy_auth=(), timeout=None, headers={}):
+                 proxy=(), rdns=True, proxy_auth=(), timeout=None, headers=None,
+                 disable_ssl_verification=False):
         """Intialize HTTP connection"""
         self.connection = None
         self.method = method
@@ -46,7 +38,8 @@
         self.rdns = rdns
         self.proxy_auth = proxy_auth
         self.timeout = timeout
-        self.headers = headers
+        self.headers = headers or {}
+        self.disable_ssl_verification = disable_ssl_verification
         if 'User-Agent' not in headers:
             self.headers['User-Agent'] = 'ZTFY HTTP Client/1.0'
 
@@ -61,7 +54,8 @@
                                             proxy_pass=self.proxy_auth and self.proxy_auth[1] or None)
         else:
             proxy_info = None
-        http = httplib2.Http(timeout=self.timeout, proxy_info=proxy_info)
+        http = httplib2.Http(timeout=self.timeout, proxy_info=proxy_info,
+                             disable_ssl_certificate_validation=self.disable_ssl_verification)
         if self.credentials:
             http.add_credentials(self.credentials[0], self.credentials[1])
         uri = '%s://%s%s' % (self.protocol, self.servername, self.url)
@@ -71,15 +65,18 @@
         return response, content
 
 
-def getClient(method, protocol, servername, url, params={}, credentials=(), proxy=(),
-              rdns=True, proxy_auth=(), timeout=None, headers={}):
+def getClient(method, protocol, servername, url, params=None, credentials=(), proxy=(),
+              rdns=True, proxy_auth=(), timeout=None, headers=None, disable_ssl_verification=False):
     """HTTP client factory"""
-    return HTTPClient(method, protocol, servername, url, params, credentials, proxy,
-                      rdns, proxy_auth, timeout, headers)
+    return HTTPClient(method, protocol, servername, url, params or {}, credentials, proxy,
+                      rdns, proxy_auth, timeout, headers or {},
+                      disable_ssl_verification)
 
 
-def getClientFromURL(url, credentials=(), proxy=(), rdns=True, proxy_auth=(), timeout=None, headers={}):
+def getClientFromURL(url, credentials=(), proxy=(), rdns=True, proxy_auth=(), timeout=None,
+                     headers=None, disable_ssl_verification=False):
     """HTTP client factory from URL"""
     elements = urlparse.urlparse(url)
     return HTTPClient('GET', elements.scheme, elements.netloc, elements.path, elements.params,
-                      credentials, proxy, rdns, proxy_auth, timeout, headers)
+                      credentials, proxy, rdns, proxy_auth, timeout, headers or {},
+                      disable_ssl_verification)