Python urllib3.make_headers() Examples
The following are 9
code examples of urllib3.make_headers().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
urllib3
, or try the search function
.
Example #1
Source File: net.py From rally with Apache License 2.0 | 6 votes |
def init(): logger = logging.getLogger(__name__) global __HTTP proxy_url = os.getenv("http_proxy") if proxy_url and len(proxy_url) > 0: parsed_url = urllib3.util.parse_url(proxy_url) logger.info("Connecting via proxy URL [%s] to the Internet (picked up from the env variable [http_proxy]).", proxy_url) __HTTP = urllib3.ProxyManager(proxy_url, cert_reqs='CERT_REQUIRED', ca_certs=certifi.where(), # appropriate headers will only be set if there is auth info proxy_headers=urllib3.make_headers(proxy_basic_auth=parsed_url.auth)) else: logger.info("Connecting directly to the Internet (no proxy support).") __HTTP = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where())
Example #2
Source File: api.py From telepot with MIT License | 6 votes |
def set_proxy(url, basic_auth=None): """ Access Bot API through a proxy. :param url: proxy URL :param basic_auth: 2-tuple ``('username', 'password')`` """ global _pools, _onetime_pool_spec if not url: _pools['default'] = urllib3.PoolManager(**_default_pool_params) _onetime_pool_spec = (urllib3.PoolManager, _onetime_pool_params) elif basic_auth: h = urllib3.make_headers(proxy_basic_auth=':'.join(basic_auth)) _pools['default'] = urllib3.ProxyManager(url, proxy_headers=h, **_default_pool_params) _onetime_pool_spec = (urllib3.ProxyManager, dict(proxy_url=url, proxy_headers=h, **_onetime_pool_params)) else: _pools['default'] = urllib3.ProxyManager(url, **_default_pool_params) _onetime_pool_spec = (urllib3.ProxyManager, dict(proxy_url=url, **_onetime_pool_params))
Example #3
Source File: http_urllib3.py From splunk-elasticsearch with Apache License 2.0 | 5 votes |
def __init__(self, host='localhost', port=9200, http_auth=None, use_ssl=False, verify_certs=False, ca_certs=None, client_cert=None, maxsize=10, **kwargs): super(Urllib3HttpConnection, self).__init__(host=host, port=port, **kwargs) self.headers = {} if http_auth is not None: if isinstance(http_auth, (tuple, list)): http_auth = ':'.join(http_auth) self.headers = urllib3.make_headers(basic_auth=http_auth) pool_class = urllib3.HTTPConnectionPool kw = {} if use_ssl: pool_class = urllib3.HTTPSConnectionPool if verify_certs: kw['cert_reqs'] = 'CERT_REQUIRED' kw['ca_certs'] = ca_certs kw['cert_file'] = client_cert elif ca_certs: raise ImproperlyConfigured("You cannot pass CA certificates when verify SSL is off.") else: warnings.warn( 'Connecting to %s using SSL with verify_certs=False is insecure.' % host) self.pool = pool_class(host, port=port, timeout=self.timeout, maxsize=maxsize, **kw)
Example #4
Source File: http_urllib3.py From sublime-elasticsearch-client with MIT License | 5 votes |
def __init__(self, host='localhost', port=9200, http_auth=None, use_ssl=False, verify_certs=False, ca_certs=None, client_cert=None, maxsize=10, **kwargs): super(Urllib3HttpConnection, self).__init__(host=host, port=port, **kwargs) self.headers = {} if http_auth is not None: if isinstance(http_auth, (tuple, list)): http_auth = ':'.join(http_auth) self.headers = urllib3.make_headers(basic_auth=http_auth) pool_class = urllib3.HTTPConnectionPool kw = {} if use_ssl: pool_class = urllib3.HTTPSConnectionPool if verify_certs: kw['cert_reqs'] = 'CERT_REQUIRED' kw['ca_certs'] = ca_certs kw['cert_file'] = client_cert elif ca_certs: raise ImproperlyConfigured("You cannot pass CA certificates when verify SSL is off.") else: warnings.warn( 'Connecting to %s using SSL with verify_certs=False is insecure.' % host) self.pool = pool_class(host, port=port, timeout=self.timeout, maxsize=maxsize, **kw)
Example #5
Source File: api.py From td-client-python with Apache License 2.0 | 5 votes |
def _init_http_proxy(self, http_proxy, **kwargs): pool_options = dict(kwargs) p = urlparse.urlparse(http_proxy) scheme = p.scheme netloc = p.netloc if "@" in netloc: auth, netloc = netloc.split("@", 2) pool_options["proxy_headers"] = urllib3.make_headers(proxy_basic_auth=auth) return urllib3.ProxyManager("%s://%s" % (scheme, netloc), **pool_options)
Example #6
Source File: consul.py From patroni with MIT License | 5 votes |
def __getattr__(self, method): if method not in ('get', 'post', 'put', 'delete'): raise AttributeError("HTTPClient instance has no attribute '{0}'".format(method)) def wrapper(callback, path, params=None, data=''): # python-consul doesn't allow to specify ttl smaller then 10 seconds # because session_ttl_min defaults to 10s, so we have to do this ugly dirty hack... if method == 'put' and path == '/v1/session/create': ttl = '"ttl": "{0}s"'.format(self._ttl) if not data or data == '{}': data = '{' + ttl + '}' else: data = data[:-1] + ', ' + ttl + '}' if isinstance(params, list): # starting from v1.1.0 python-consul switched from `dict` to `list` for params params = {k: v for k, v in params} kwargs = {'retries': 0, 'preload_content': False, 'body': data} if method == 'get' and isinstance(params, dict) and 'index' in params: timeout = float(params['wait'][:-1]) if 'wait' in params else 300 # According to the documentation a small random amount of additional wait time is added to the # supplied maximum wait time to spread out the wake up time of any concurrent requests. This adds # up to wait / 16 additional time to the maximum duration. Since our goal is actually getting a # response rather read timeout we will add to the timeout a sligtly bigger value. kwargs['timeout'] = timeout + max(timeout/15.0, 1) else: kwargs['timeout'] = self._read_timeout token = params.pop('token', self.token) if isinstance(params, dict) else self.token kwargs['headers'] = urllib3.make_headers(user_agent=USER_AGENT) if token: kwargs['headers']['X-Consul-Token'] = token return callback(self.response(self.http.request(method.upper(), self.uri(path, params), **kwargs))) return wrapper
Example #7
Source File: request.py From patroni with MIT License | 5 votes |
def reload_config(self, config): self._pool.headers = urllib3.make_headers(basic_auth=self._get_cfg_value(config, 'auth'), user_agent=USER_AGENT) if self._apply_ssl_file_param(config, 'cert'): self._apply_ssl_file_param(config, 'key') else: self._pool.connection_pool_kw.pop('key_file', None) cacert = config.get('ctl', {}).get('cacert') or config.get('restapi', {}).get('cafile') self._apply_pool_param('ca_certs', cacert)
Example #8
Source File: http_urllib3.py From watchmen with Apache License 2.0 | 4 votes |
def __init__(self, host='localhost', port=9200, http_auth=None, use_ssl=False, verify_certs=True, ca_certs=None, client_cert=None, client_key=None, ssl_version=None, ssl_assert_hostname=None, ssl_assert_fingerprint=None, maxsize=10, headers=None, **kwargs): super(Urllib3HttpConnection, self).__init__(host=host, port=port, use_ssl=use_ssl, **kwargs) self.headers = urllib3.make_headers(keep_alive=True) if http_auth is not None: if isinstance(http_auth, (tuple, list)): http_auth = ':'.join(http_auth) self.headers.update(urllib3.make_headers(basic_auth=http_auth)) # update headers in lowercase to allow overriding of auth headers if headers: for k in headers: self.headers[k.lower()] = headers[k] self.headers.setdefault('content-type', 'application/json') ca_certs = CA_CERTS if ca_certs is None else ca_certs pool_class = urllib3.HTTPConnectionPool kw = {} if use_ssl: pool_class = urllib3.HTTPSConnectionPool kw.update({ 'ssl_version': ssl_version, 'assert_hostname': ssl_assert_hostname, 'assert_fingerprint': ssl_assert_fingerprint, }) if verify_certs: if not ca_certs: raise ImproperlyConfigured("Root certificates are missing for certificate " "validation. Either pass them in using the ca_certs parameter or " "install certifi to use it automatically.") kw.update({ 'cert_reqs': 'CERT_REQUIRED', 'ca_certs': ca_certs, 'cert_file': client_cert, 'key_file': client_key, }) else: warnings.warn( 'Connecting to %s using SSL with verify_certs=False is insecure.' % host) self.pool = pool_class(host, port=port, timeout=self.timeout, maxsize=maxsize, **kw)
Example #9
Source File: api.py From Tautulli with GNU General Public License v3.0 | 4 votes |
def _call_api(method, uri, params=None, body=None, headers=None, **options): prefix = options.pop("upload_prefix", cloudinary.config().upload_prefix) or "https://api.cloudinary.com" cloud_name = options.pop("cloud_name", cloudinary.config().cloud_name) if not cloud_name: raise Exception("Must supply cloud_name") api_key = options.pop("api_key", cloudinary.config().api_key) if not api_key: raise Exception("Must supply api_key") api_secret = options.pop("api_secret", cloudinary.config().api_secret) if not cloud_name: raise Exception("Must supply api_secret") api_url = "/".join([prefix, "v1_1", cloud_name] + uri) processed_params = None if isinstance(params, dict): processed_params = {} for key, value in params.items(): if isinstance(value, list) or isinstance(value, tuple): value_list = {"{}[{}]".format(key, i): i_value for i, i_value in enumerate(value)} processed_params.update(value_list) elif value: processed_params[key] = value # Add authentication req_headers = urllib3.make_headers( basic_auth="{0}:{1}".format(api_key, api_secret), user_agent=cloudinary.get_user_agent() ) if headers is not None: req_headers.update(headers) kw = {} if 'timeout' in options: kw['timeout'] = options['timeout'] if body is not None: kw['body'] = body try: response = _http.request(method.upper(), api_url, processed_params, req_headers, **kw) body = response.data except HTTPError as e: raise GeneralError("Unexpected error {0}", e.message) except socket.error as e: raise GeneralError("Socket Error: %s" % (str(e))) try: result = json.loads(body.decode('utf-8')) except Exception as e: # Error is parsing json raise GeneralError("Error parsing server response (%d) - %s. Got - %s" % (response.status, body, e)) if "error" in result: exception_class = EXCEPTION_CODES.get(response.status) or Exception exception_class = exception_class raise exception_class("Error {0} - {1}".format(response.status, result["error"]["message"])) return Response(result, response)