src/pyams_utils/protocol/http.py
branchdev-tf
changeset 427 63284c98cdc1
parent 292 b338586588ad
equal deleted inserted replaced
426:2022e4da3ad9 427:63284c98cdc1
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
    10 # FOR A PARTICULAR PURPOSE.
    10 # FOR A PARTICULAR PURPOSE.
    11 #
    11 #
    12 
    12 
       
    13 """PyAMS_utils.protocol.http module
       
    14 
       
    15 This module provides an HTTP client class, which allows to easilly define proxies and
       
    16 authentication headers.
       
    17 """
       
    18 
       
    19 import urllib.parse
       
    20 
       
    21 import httplib2
       
    22 
       
    23 
    13 __docformat__ = 'restructuredtext'
    24 __docformat__ = 'restructuredtext'
    14 
    25 
    15 
    26 
    16 # import standard library
    27 class HTTPClient:
    17 import httplib2
    28     # pylint: disable=too-many-instance-attributes
    18 import urllib.parse
       
    19 
       
    20 # import interfaces
       
    21 
       
    22 # import packages
       
    23 
       
    24 
       
    25 class HTTPClient(object):
       
    26     """HTTP client with proxy support"""
    29     """HTTP client with proxy support"""
    27 
    30 
    28     def __init__(self, method, protocol, servername, url, params={}, credentials=(),
    31     def __init__(self, method, protocol, servername, url, params=None, credentials=(),
    29                  proxy=(), rdns=True, proxy_auth=(), timeout=None, headers={}):
    32                  proxy=(), rdns=True, proxy_auth=(), timeout=None, headers=None):
       
    33         # pylint: disable=too-many-arguments
    30         """Intialize HTTP connection"""
    34         """Intialize HTTP connection"""
    31         self.connection = None
    35         self.connection = None
    32         self.method = method
    36         self.method = method
    33         self.protocol = protocol
    37         self.protocol = protocol
    34         self.servername = servername
    38         self.servername = servername
    35         self.url = url
    39         self.url = url
    36         self.params = params
    40         self.params = params or {}
    37         self.location = None
    41         self.location = None
    38         self.credentials = credentials
    42         self.credentials = credentials
    39         self.proxy = proxy
    43         self.proxy = proxy
    40         self.rdns = rdns
    44         self.rdns = rdns
    41         self.proxy_auth = proxy_auth
    45         self.proxy_auth = proxy_auth
    42         self.timeout = timeout
    46         self.timeout = timeout
    43         self.headers = headers
    47         self.headers = headers or {}
    44         if 'User-Agent' not in headers:
    48         if 'User-Agent' not in self.headers:
    45             self.headers['User-Agent'] = 'PyAMS HTTP Client/1.0'
    49             self.headers['User-Agent'] = 'PyAMS HTTP Client/1.0'
    46 
    50 
    47     def get_response(self):
    51     def get_response(self):
    48         """Common HTTP request"""
    52         """Common HTTP request"""
    49         if self.proxy and (len(self.proxy) == 2):
    53         if self.proxy and (len(self.proxy) == 2):
    50             proxy_info = httplib2.ProxyInfo(httplib2.socks.PROXY_TYPE_HTTP,
    54             proxy_info = httplib2.ProxyInfo(
    51                                             proxy_host=self.proxy[0],
    55                 httplib2.socks.PROXY_TYPE_HTTP,
    52                                             proxy_port=self.proxy[1],
    56                 proxy_host=self.proxy[0],
    53                                             proxy_rdns=self.rdns,
    57                 proxy_port=self.proxy[1],
    54                                             proxy_user=self.proxy_auth and self.proxy_auth[0] or None,
    58                 proxy_rdns=self.rdns,
    55                                             proxy_pass=self.proxy_auth and self.proxy_auth[1] or None)
    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)
    56         else:
    61         else:
    57             proxy_info = None
    62             proxy_info = None
    58         http = httplib2.Http(timeout=self.timeout, proxy_info=proxy_info)
    63         http = httplib2.Http(timeout=self.timeout, proxy_info=proxy_info)
    59         if self.credentials:
    64         if self.credentials:
    60             http.add_credentials(self.credentials[0], self.credentials[1])
    65             http.add_credentials(self.credentials[0], self.credentials[1])
    63             uri += '?' + urllib.parse.urlencode(self.params)
    68             uri += '?' + urllib.parse.urlencode(self.params)
    64         response, content = http.request(uri, self.method, headers=self.headers)
    69         response, content = http.request(uri, self.method, headers=self.headers)
    65         return response, content
    70         return response, content
    66 
    71 
    67 
    72 
    68 def get_client(method, protocol, servername, url, params={}, credentials=(), proxy=(),
    73 def get_client(method, protocol, servername, url, params=None, credentials=(), proxy=(),
    69                rdns=True, proxy_auth=(), timeout=None, headers={}):
    74                rdns=True, proxy_auth=(), timeout=None, headers=None):
       
    75     # pylint: disable=too-many-arguments
    70     """HTTP client factory"""
    76     """HTTP client factory"""
    71     return HTTPClient(method, protocol, servername, url, params, credentials, proxy,
    77     return HTTPClient(method, protocol, servername, url, params, credentials, proxy,
    72                       rdns, proxy_auth, timeout, headers)
    78                       rdns, proxy_auth, timeout, headers)
    73 
    79 
    74 
    80 
    75 def get_client_from_url(url, credentials=(), proxy=(), rdns=True, proxy_auth=(), timeout=None, headers={}):
    81 def get_client_from_url(url, credentials=(), proxy=(), rdns=True, proxy_auth=(), timeout=None,
       
    82                         headers=None):
       
    83     # pylint: disable=too-many-arguments
    76     """HTTP client factory from URL"""
    84     """HTTP client factory from URL"""
    77     elements = urllib.parse.urlparse(url)
    85     elements = urllib.parse.urlparse(url)
    78     return HTTPClient('GET', elements.scheme, elements.netloc, elements.path, elements.params,
    86     return HTTPClient('GET', elements.scheme, elements.netloc, elements.path, elements.params,
    79                       credentials, proxy, rdns, proxy_auth, timeout, headers)
    87                       credentials, proxy, rdns, proxy_auth, timeout, headers)