1 ### -*- coding: utf-8 -*- #################################################### |
|
2 ############################################################################## |
|
3 # |
|
4 # Copyright (c) 2012 Thierry Florac <tflorac AT ulthar.net> |
|
5 # All Rights Reserved. |
|
6 # |
|
7 # This software is subject to the provisions of the Zope Public License, |
|
8 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|
9 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|
10 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
11 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|
12 # FOR A PARTICULAR PURPOSE. |
|
13 # |
|
14 ############################################################################## |
|
15 |
|
16 |
|
17 # import standard packages |
|
18 import httplib2 |
|
19 import urllib |
|
20 |
|
21 # import Zope3 interfaces |
|
22 |
|
23 # import local interfaces |
|
24 |
|
25 # import Zope3 packages |
|
26 |
|
27 # import local packages |
|
28 |
|
29 |
|
30 class HTTPClient(object): |
|
31 """HTTP client""" |
|
32 |
|
33 def __init__(self, method, protocol, servername, url, params={}, credentials=(), |
|
34 proxy=(), rdns=True, proxy_auth=(), timeout=None, headers={}): |
|
35 """Intialize HTTP connection""" |
|
36 self.connection = None |
|
37 self.method = method |
|
38 self.protocol = protocol |
|
39 self.servername = servername |
|
40 self.url = url |
|
41 self.params = params |
|
42 self.location = None |
|
43 self.credentials = credentials |
|
44 self.proxy = proxy |
|
45 self.rdns = rdns |
|
46 self.proxy_auth = proxy_auth |
|
47 self.timeout = timeout |
|
48 self.headers = headers |
|
49 if 'User-Agent' not in headers: |
|
50 self.headers['User-Agent'] = 'ZTFY HTTP Client/1.0' |
|
51 |
|
52 def getResponse(self): |
|
53 """Common HTTP request""" |
|
54 if self.proxy: |
|
55 proxy_info = httplib2.ProxyInfo(httplib2.socks.PROXY_TYPE_HTTP, |
|
56 proxy_host=self.proxy[0], |
|
57 proxy_port=self.proxy[1], |
|
58 proxy_rdns=self.rdns, |
|
59 proxy_user=self.proxy_auth and self.proxy_auth[0] or None, |
|
60 proxy_pass=self.proxy_auth and self.proxy_auth[1] or None) |
|
61 proxy_info = None |
|
62 else: |
|
63 proxy_info = None |
|
64 http = httplib2.Http(timeout=self.timeout, proxy_info=proxy_info) |
|
65 if self.credentials: |
|
66 http.add_credentials(self.credentials[0], self.credentials[1]) |
|
67 uri = '%s://%s%s' % (self.protocol, self.servername, self.url) |
|
68 if self.params: |
|
69 uri += '?' + urllib.urlencode(self.params) |
|
70 response, content = http.request(uri, self.method, headers=self.headers) |
|
71 return response, content |
|
72 |
|
73 |
|
74 def getClient(method, protocol, servername, url, params={}, credentials=(), proxy=(), rdns=True, proxy_auth=(), timeout=None, headers={}): |
|
75 """HTTP client factory""" |
|
76 return HTTPClient(method, protocol, servername, url, params, credentials, proxy, rdns, proxy_auth, timeout, headers) |
|