11 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
11 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
12 # FOR A PARTICULAR PURPOSE. |
12 # FOR A PARTICULAR PURPOSE. |
13 # |
13 # |
14 ############################################################################## |
14 ############################################################################## |
15 |
15 |
16 |
|
17 # import standard packages |
|
18 import httplib2 |
|
19 import urllib |
16 import urllib |
20 import urlparse |
17 import urlparse |
21 |
18 |
22 # import Zope3 interfaces |
19 import httplib2 |
23 |
|
24 # import local interfaces |
|
25 |
|
26 # import Zope3 packages |
|
27 |
|
28 # import local packages |
|
29 |
20 |
30 |
21 |
31 class HTTPClient(object): |
22 class HTTPClient(object): |
32 """HTTP client""" |
23 """HTTP client""" |
33 |
24 |
34 def __init__(self, method, protocol, servername, url, params={}, credentials=(), |
25 def __init__(self, method, protocol, servername, url, params={}, credentials=(), |
35 proxy=(), rdns=True, proxy_auth=(), timeout=None, headers={}): |
26 proxy=(), rdns=True, proxy_auth=(), timeout=None, headers=None, |
|
27 disable_ssl_verification=False): |
36 """Intialize HTTP connection""" |
28 """Intialize HTTP connection""" |
37 self.connection = None |
29 self.connection = None |
38 self.method = method |
30 self.method = method |
39 self.protocol = protocol |
31 self.protocol = protocol |
40 self.servername = servername |
32 self.servername = servername |
59 proxy_rdns=self.rdns, |
52 proxy_rdns=self.rdns, |
60 proxy_user=self.proxy_auth and self.proxy_auth[0] or None, |
53 proxy_user=self.proxy_auth and self.proxy_auth[0] or None, |
61 proxy_pass=self.proxy_auth and self.proxy_auth[1] or None) |
54 proxy_pass=self.proxy_auth and self.proxy_auth[1] or None) |
62 else: |
55 else: |
63 proxy_info = None |
56 proxy_info = None |
64 http = httplib2.Http(timeout=self.timeout, proxy_info=proxy_info) |
57 http = httplib2.Http(timeout=self.timeout, proxy_info=proxy_info, |
|
58 disable_ssl_certificate_validation=self.disable_ssl_verification) |
65 if self.credentials: |
59 if self.credentials: |
66 http.add_credentials(self.credentials[0], self.credentials[1]) |
60 http.add_credentials(self.credentials[0], self.credentials[1]) |
67 uri = '%s://%s%s' % (self.protocol, self.servername, self.url) |
61 uri = '%s://%s%s' % (self.protocol, self.servername, self.url) |
68 if self.params: |
62 if self.params: |
69 uri += '?' + urllib.urlencode(self.params) |
63 uri += '?' + urllib.urlencode(self.params) |
70 response, content = http.request(uri, self.method, headers=self.headers) |
64 response, content = http.request(uri, self.method, headers=self.headers) |
71 return response, content |
65 return response, content |
72 |
66 |
73 |
67 |
74 def getClient(method, protocol, servername, url, params={}, credentials=(), proxy=(), |
68 def getClient(method, protocol, servername, url, params=None, credentials=(), proxy=(), |
75 rdns=True, proxy_auth=(), timeout=None, headers={}): |
69 rdns=True, proxy_auth=(), timeout=None, headers=None, disable_ssl_verification=False): |
76 """HTTP client factory""" |
70 """HTTP client factory""" |
77 return HTTPClient(method, protocol, servername, url, params, credentials, proxy, |
71 return HTTPClient(method, protocol, servername, url, params or {}, credentials, proxy, |
78 rdns, proxy_auth, timeout, headers) |
72 rdns, proxy_auth, timeout, headers or {}, |
|
73 disable_ssl_verification) |
79 |
74 |
80 |
75 |
81 def getClientFromURL(url, credentials=(), proxy=(), rdns=True, proxy_auth=(), timeout=None, headers={}): |
76 def getClientFromURL(url, credentials=(), proxy=(), rdns=True, proxy_auth=(), timeout=None, |
|
77 headers=None, disable_ssl_verification=False): |
82 """HTTP client factory from URL""" |
78 """HTTP client factory from URL""" |
83 elements = urlparse.urlparse(url) |
79 elements = urlparse.urlparse(url) |
84 return HTTPClient('GET', elements.scheme, elements.netloc, elements.path, elements.params, |
80 return HTTPClient('GET', elements.scheme, elements.netloc, elements.path, elements.params, |
85 credentials, proxy, rdns, proxy_auth, timeout, headers) |
81 credentials, proxy, rdns, proxy_auth, timeout, headers or {}, |
|
82 disable_ssl_verification) |