Python urllib3.ProxyManager() Examples

The following are 30 code examples for showing how to use urllib3.ProxyManager(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module urllib3 , or try the search function .

Example 1
Project: SharPyShell   Author: antonioCoco   File: Request.py    License: GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, url, user_agent, cookies_string=False, custom_header=False, insecure_ssl='false', proxy=False):
        urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
        self.__url = url
        self.__headers = dict()
        self.__headers['User-Agent'] = self.__default_user_agent if user_agent == 'default' else user_agent
        if cookies_string:
            self.__headers['Cookie'] = cookies_string
        if custom_header:
            self.__parse_custom_header(custom_header)
        self.__verify = 'CERT_REQUIRED' if insecure_ssl == 'false' else 'CERT_NONE'
        if proxy:
            proxy_type = proxy.split('://')[0]
            if proxy_type == 'http' or proxy_type == 'https':
                self.__request_obj = urllib3.ProxyManager(proxy, ssl_version=ssl.PROTOCOL_TLSv1,
                                                          timeout=self.__request_timeout, cert_reqs=self.__verify)
            else:
                self.__request_obj = SOCKSProxyManager(proxy, ssl_version=ssl.PROTOCOL_TLSv1,
                                                       timeout=self.__request_timeout, cert_reqs=self.__verify)
        else:
            self.__request_obj = urllib3.PoolManager(ssl_version=ssl.PROTOCOL_TLSv1, timeout=self.__request_timeout,
                                                     cert_reqs=self.__verify)
        # print (vars(self)) 
Example 2
Project: rally   Author: elastic   File: net.py    License: Apache License 2.0 6 votes vote down vote up
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 3
Project: apm-agent-python   Author: elastic   File: http.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, url, *args, **kwargs):
        super(Transport, self).__init__(url, *args, **kwargs)
        url_parts = compat.urlparse.urlparse(url)
        pool_kwargs = {"cert_reqs": "CERT_REQUIRED", "ca_certs": certifi.where(), "block": True}
        if self._server_cert and url_parts.scheme != "http":
            pool_kwargs.update(
                {"assert_fingerprint": self.cert_fingerprint, "assert_hostname": False, "cert_reqs": ssl.CERT_NONE}
            )
            del pool_kwargs["ca_certs"]
        elif not self._verify_server_cert and url_parts.scheme != "http":
            pool_kwargs["cert_reqs"] = ssl.CERT_NONE
            pool_kwargs["assert_hostname"] = False
        proxies = compat.getproxies_environment()
        proxy_url = proxies.get("https", proxies.get("http", None))
        if proxy_url and not compat.proxy_bypass_environment(url_parts.netloc):
            self.http = urllib3.ProxyManager(proxy_url, **pool_kwargs)
        else:
            self.http = urllib3.PoolManager(**pool_kwargs) 
Example 4
Project: telepot   Author: nickoala   File: api.py    License: MIT License 6 votes vote down vote up
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 5
Project: ProxHTTPSProxyMII   Author: wheever   File: ProxHTTPSProxy.py    License: MIT License 5 votes vote down vote up
def setProxyPool(self, proxy):
        scheme = proxy.split(':')[0]
        if scheme in ('http', 'https'):
            ProxyManager = urllib3.ProxyManager
        elif scheme in ('socks4', 'socks5'):
            ProxyManager = SOCKSProxyManager
        else:
            print("Wrong Proxy Format: " + proxy)
            print("Proxy should start with http/https/socks4/socks5 .")
            input()
            raise SystemExit
        # maxsize is the max. number of connections to the same server
        return [ProxyManager(proxy, num_pools=10, maxsize=8, timeout=self.timeout, **self.sslparams),
                ProxyManager(proxy, num_pools=10, maxsize=8, timeout=self.timeout)] 
Example 6
Project: bilibiliupload   Author: ForgQi   File: rangefetch_server.py    License: MIT License 5 votes vote down vote up
def __init__(self, handler, range_start, range_end):
        self.handler = handler
        self.write = handler.wfile.write
        self.url = handler.url
        self.scheme = handler.url_parts.scheme
        self.netloc = handler.url_parts.netloc
        self.headers = dict((k.title(), v) for k, v in handler.headers.items())
        self.headers['Host'] = self.netloc
        self.headers.update(fake_headers)

        self.range_start = range_start
        self.range_end = range_end
        self.delay_cache_size = self.max_size * self.threads * 4
        self.delay_star_size = self.delay_cache_size * 2
        self.max_threads = min(self.threads * 2, self.pool_size)

        if self.http is None:
            connection_pool_kw = {
                'block': True,
                'timeout': self.timeout,
                'maxsize': self.pool_size
            }
            if self.proxy:
                if self.proxy.lower().startswith('socks'):
                    from urllib3.contrib.socks import SOCKSProxyManager as ProxyManager
                else:
                    ProxyManager = urllib3.ProxyManager
                http = ProxyManager(self.proxy, **connection_pool_kw)
                if ProxyManager is not urllib3.ProxyManager:
                    http.connection_pool_kw['_socks_options']['rdns'] = True
            else:
                http = urllib3.PoolManager(**connection_pool_kw)
            self.__class__.http = http

        self.firstrange = range_start, range_start + self.first_size - 1

        self.data_queue = Queue.PriorityQueue()
        self.range_queue = Queue.LifoQueue()
        self._started_threads = {} 
Example 7
Project: slpkg   Author: dslackw   File: file_size.py    License: GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, registry):
        self.meta = _meta_
        self.registry = registry
        if self.meta.http_proxy:
            self.http = urllib3.ProxyManager(self.meta.http_proxy)
        else:
            self.http = urllib3.PoolManager() 
Example 8
Project: slpkg   Author: dslackw   File: url_read.py    License: GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, link):
        self.link = link
        self.meta = _meta_
        self.red = _meta_.color["RED"]
        self.endc = _meta_.color["ENDC"]
        if self.meta.http_proxy:
            self.http = urllib3.ProxyManager(self.meta.http_proxy)
        else:
            self.http = urllib3.PoolManager() 
Example 9
Project: OpenDoor   Author: stanislav-web   File: proxy.py    License: GNU General Public License v3.0 5 votes vote down vote up
def __proxy_pool(self):
        """
        Create Proxy connection pool
        :raise ProxyRequestError
        :return: urllib3.HTTPConnectionPool
        """

        try:

            self.__server = self.__cfg.proxy if True is self.__cfg.is_standalone_proxy else self.__get_random_proxy()

            if self.__get_proxy_type(self.__server) == 'socks':

                disable_warnings(InsecureRequestWarning)

                if not hasattr(self, '__pm'):

                    package_module = importlib.import_module('urllib3.contrib.socks')
                    self.__pm = getattr(package_module, 'SOCKSProxyManager')

                pool = self.__pm(self.__server,
                                 num_pools=self.__cfg.threads,
                                 timeout=Timeout(self.__cfg.timeout,
                                 read=self.__cfg.timeout),
                                 block=True)
            else:
                pool = ProxyManager(self.__server,
                                    num_pools=self.__cfg.threads,
                                    timeout=Timeout(self.__cfg.timeout, read=self.__cfg.timeout),
                                    block=True)
            return pool
        except (DependencyWarning, ProxySchemeUnknown, ImportError) as error:
            raise ProxyRequestError(error) 
Example 10
Project: hoverpy   Author: SpectoLabs   File: __init__.py    License: Apache License 2.0 5 votes vote down vote up
def ProxyManager():
        import urllib3
        proxy = urllib3.ProxyManager('http://localhost:8500/')
        return proxy 
Example 11
Project: cloud-custodian   Author: cloud-custodian   File: webhook.py    License: Apache License 2.0 5 votes vote down vote up
def _build_http_manager(self):
        pool_kwargs = {
            'cert_reqs': 'CERT_REQUIRED',
            'ca_certs': certifi and certifi.where() or None
        }

        proxy_url = utils.get_proxy_url(self.url)
        if proxy_url:
            return urllib3.ProxyManager(proxy_url, **pool_kwargs)
        else:
            return urllib3.PoolManager(**pool_kwargs) 
Example 12
Project: google-search-telegram-bot   Author: nkming2   File: paw_app.py    License: Apache License 2.0 5 votes vote down vote up
def _init_paw_telepot(self):
		# You can leave this bit out if you're using a paid PythonAnywhere
		# account
		proxy_url = "http://proxy.server:3128"
		telepot.api._pools = {
			"default": urllib3.ProxyManager(proxy_url = proxy_url, num_pools = 3,
					maxsize = 10, retries = False, timeout = 30),
		}
		telepot.api._onetime_pool_spec = (urllib3.ProxyManager,
				dict(proxy_url = proxy_url, num_pools = 1, maxsize = 1,
						retries = False, timeout = 30))
		# end of the stuff that's only needed for free accounts 
Example 13
Project: td-client-python   Author: treasure-data   File: api.py    License: Apache License 2.0 5 votes vote down vote up
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 14
Project: td-client-python   Author: treasure-data   File: api_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_http_proxy_from_environ():
    os.environ["HTTP_PROXY"] = "proxy1.example.com:8080"
    td = api.API("apikey")
    assert isinstance(td.http, urllib3.ProxyManager)
    assert td.http.proxy.url == "http://proxy1.example.com:8080"
    assert td.http.proxy_headers == {} 
Example 15
Project: td-client-python   Author: treasure-data   File: api_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_http_proxy_from_keyword():
    td = api.API("apikey", http_proxy="proxy2.example.com:8080")
    assert isinstance(td.http, urllib3.ProxyManager)
    assert td.http.proxy.url == "http://proxy2.example.com:8080"
    assert td.http.proxy_headers == {} 
Example 16
Project: td-client-python   Author: treasure-data   File: api_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_http_proxy_prefer_keyword():
    os.environ["HTTP_PROXY"] = "proxy1.example.com:8080"
    td = api.API("apikey", http_proxy="proxy2.example.com:8080")
    assert isinstance(td.http, urllib3.ProxyManager)
    assert td.http.proxy.url == "http://proxy2.example.com:8080"
    assert td.http.proxy_headers == {} 
Example 17
Project: td-client-python   Author: treasure-data   File: api_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_http_proxy_with_scheme():
    td = api.API("apikey", http_proxy="http://proxy1.example.com:8080/")
    assert isinstance(td.http, urllib3.ProxyManager)
    assert td.http.proxy.url == "http://proxy1.example.com:8080"
    assert td.http.proxy_headers == {} 
Example 18
Project: td-client-python   Author: treasure-data   File: api_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_http_proxy_with_scheme_and_credentials():
    td = api.API("apikey", http_proxy="http://john:doe@proxy1.example.com:8080/")
    assert isinstance(td.http, urllib3.ProxyManager)
    assert td.http.proxy.url == "http://proxy1.example.com:8080"
    assert td.http.proxy_headers == {"proxy-authorization": "Basic am9objpkb2U="} 
Example 19
Project: httplib2shim   Author: GoogleCloudPlatform   File: __init__.py    License: MIT License 5 votes vote down vote up
def _default_make_pool(http, proxy_info):
    """Creates a urllib3.PoolManager object that has SSL verification enabled
    and uses the certifi certificates."""

    if not http.ca_certs:
        http.ca_certs = _certifi_where_for_ssl_version()

    ssl_disabled = http.disable_ssl_certificate_validation

    cert_reqs = 'CERT_REQUIRED' if http.ca_certs and not ssl_disabled else None

    if isinstance(proxy_info, collections.Callable):
        proxy_info = proxy_info()
    if proxy_info:
        if proxy_info.proxy_user and proxy_info.proxy_pass:
            proxy_url = 'http://{}:{}@{}:{}/'.format(
                proxy_info.proxy_user, proxy_info.proxy_pass,
                proxy_info.proxy_host, proxy_info.proxy_port,
            )
            proxy_headers = urllib3.util.request.make_headers(
                proxy_basic_auth='{}:{}'.format(
                    proxy_info.proxy_user, proxy_info.proxy_pass,
                )
            )
        else:
            proxy_url = 'http://{}:{}/'.format(
                proxy_info.proxy_host, proxy_info.proxy_port,
            )
            proxy_headers = {}

        return urllib3.ProxyManager(
            proxy_url=proxy_url,
            proxy_headers=proxy_headers,
            ca_certs=http.ca_certs,
            cert_reqs=cert_reqs,
        )
    return urllib3.PoolManager(
        ca_certs=http.ca_certs,
        cert_reqs=cert_reqs,
    ) 
Example 20
Project: httplib2shim   Author: GoogleCloudPlatform   File: httplib2_test.py    License: MIT License 5 votes vote down vote up
def test_instance_and_callable(self):
        proxy_info = httplib2.proxy_info_from_url('http://myproxy.example.com')
        http = httplib2.Http(proxy_info=proxy_info)
        self.assertIsInstance(http.pool, urllib3.ProxyManager)
        http = httplib2.Http(proxy_info=lambda: proxy_info)
        self.assertIsInstance(http.pool, urllib3.ProxyManager) 
Example 21
Project: Tautulli   Author: Tautulli   File: utils.py    License: GNU General Public License v3.0 5 votes vote down vote up
def get_http_connector(conf, options):
    """
    Used to create http connector, depends on api_proxy configuration parameter

    :param conf: configuration object
    :param options: additional options

    :return: ProxyManager if api_proxy is set, otherwise PoolManager object
    """
    if conf.api_proxy:
        return ProxyManager(conf.api_proxy, **options)
    else:
        return PoolManager(**options) 
Example 22
Project: faxplus-python   Author: alohi   File: rest.py    License: MIT License 4 votes vote down vote up
def __init__(self, configuration, pools_size=4, maxsize=None):
        # urllib3.PoolManager will pass all kw parameters to connectionpool
        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75  # noqa: E501
        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680  # noqa: E501
        # maxsize is the number of requests to host that are allowed in parallel  # noqa: E501
        # Custom SSL certificates and client certificates: http://urllib3.readthedocs.io/en/latest/advanced-usage.html  # noqa: E501

        # cert_reqs
        if configuration.verify_ssl:
            cert_reqs = ssl.CERT_REQUIRED
        else:
            cert_reqs = ssl.CERT_NONE

        # ca_certs
        if configuration.ssl_ca_cert:
            ca_certs = configuration.ssl_ca_cert
        else:
            # if not set certificate file, use Mozilla's root certificates.
            ca_certs = certifi.where()

        addition_pool_args = {}
        if configuration.assert_hostname is not None:
            addition_pool_args['assert_hostname'] = configuration.assert_hostname  # noqa: E501

        if maxsize is None:
            if configuration.connection_pool_maxsize is not None:
                maxsize = configuration.connection_pool_maxsize
            else:
                maxsize = 4

        # https pool manager
        if configuration.proxy:
            self.pool_manager = urllib3.ProxyManager(
                num_pools=pools_size,
                maxsize=maxsize,
                cert_reqs=cert_reqs,
                ca_certs=ca_certs,
                cert_file=configuration.cert_file,
                key_file=configuration.key_file,
                proxy_url=configuration.proxy,
                **addition_pool_args
            )
        else:
            self.pool_manager = urllib3.PoolManager(
                num_pools=pools_size,
                maxsize=maxsize,
                cert_reqs=cert_reqs,
                ca_certs=ca_certs,
                cert_file=configuration.cert_file,
                key_file=configuration.key_file,
                **addition_pool_args
            ) 
Example 23
Project: demisto-py   Author: demisto   File: rest.py    License: Apache License 2.0 4 votes vote down vote up
def __init__(self, configuration, pools_size=4, maxsize=None):
        # urllib3.PoolManager will pass all kw parameters to connectionpool
        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75  # noqa: E501
        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680  # noqa: E501
        # maxsize is the number of requests to host that are allowed in parallel  # noqa: E501
        # Custom SSL certificates and client certificates: http://urllib3.readthedocs.io/en/latest/advanced-usage.html  # noqa: E501

        # cert_reqs
        if configuration.verify_ssl:
            cert_reqs = ssl.CERT_REQUIRED
        else:
            cert_reqs = ssl.CERT_NONE

        # ca_certs
        if configuration.ssl_ca_cert:
            ca_certs = configuration.ssl_ca_cert
        else:
            # if not set certificate file, use Mozilla's root certificates.
            ca_certs = certifi.where()

        addition_pool_args = {}
        if configuration.assert_hostname is not None:
            addition_pool_args['assert_hostname'] = configuration.assert_hostname  # noqa: E501

        if maxsize is None:
            if configuration.connection_pool_maxsize is not None:
                maxsize = configuration.connection_pool_maxsize
            else:
                maxsize = 4

        # https pool manager
        if configuration.proxy:
            self.pool_manager = urllib3.ProxyManager(
                num_pools=pools_size,
                maxsize=maxsize,
                cert_reqs=cert_reqs,
                ca_certs=ca_certs,
                cert_file=configuration.cert_file,
                key_file=configuration.key_file,
                proxy_url=configuration.proxy,
                **addition_pool_args
            )
        else:
            self.pool_manager = urllib3.PoolManager(
                num_pools=pools_size,
                maxsize=maxsize,
                cert_reqs=cert_reqs,
                ca_certs=ca_certs,
                cert_file=configuration.cert_file,
                key_file=configuration.key_file,
                **addition_pool_args
            ) 
Example 24
Project: APIv3-python-library   Author: sendinblue   File: rest.py    License: MIT License 4 votes vote down vote up
def __init__(self, configuration, pools_size=4, maxsize=None):
        # urllib3.PoolManager will pass all kw parameters to connectionpool
        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75  # noqa: E501
        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680  # noqa: E501
        # maxsize is the number of requests to host that are allowed in parallel  # noqa: E501
        # Custom SSL certificates and client certificates: http://urllib3.readthedocs.io/en/latest/advanced-usage.html  # noqa: E501

        # cert_reqs
        if configuration.verify_ssl:
            cert_reqs = ssl.CERT_REQUIRED
        else:
            cert_reqs = ssl.CERT_NONE

        # ca_certs
        if configuration.ssl_ca_cert:
            ca_certs = configuration.ssl_ca_cert
        else:
            # if not set certificate file, use Mozilla's root certificates.
            ca_certs = certifi.where()

        addition_pool_args = {}
        if configuration.assert_hostname is not None:
            addition_pool_args['assert_hostname'] = configuration.assert_hostname  # noqa: E501

        if maxsize is None:
            if configuration.connection_pool_maxsize is not None:
                maxsize = configuration.connection_pool_maxsize
            else:
                maxsize = 4

        # https pool manager
        if configuration.proxy:
            self.pool_manager = urllib3.ProxyManager(
                num_pools=pools_size,
                maxsize=maxsize,
                cert_reqs=cert_reqs,
                ca_certs=ca_certs,
                cert_file=configuration.cert_file,
                key_file=configuration.key_file,
                proxy_url=configuration.proxy,
                **addition_pool_args
            )
        else:
            self.pool_manager = urllib3.PoolManager(
                num_pools=pools_size,
                maxsize=maxsize,
                cert_reqs=cert_reqs,
                ca_certs=ca_certs,
                cert_file=configuration.cert_file,
                key_file=configuration.key_file,
                **addition_pool_args
            ) 
Example 25
Project: api-python-client   Author: onfido   File: rest.py    License: MIT License 4 votes vote down vote up
def __init__(self, configuration, pools_size=4, maxsize=None):
        # urllib3.PoolManager will pass all kw parameters to connectionpool
        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75  # noqa: E501
        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680  # noqa: E501
        # maxsize is the number of requests to host that are allowed in parallel  # noqa: E501
        # Custom SSL certificates and client certificates: http://urllib3.readthedocs.io/en/latest/advanced-usage.html  # noqa: E501

        # cert_reqs
        if configuration.verify_ssl:
            cert_reqs = ssl.CERT_REQUIRED
        else:
            cert_reqs = ssl.CERT_NONE

        # ca_certs
        if configuration.ssl_ca_cert:
            ca_certs = configuration.ssl_ca_cert
        else:
            # if not set certificate file, use Mozilla's root certificates.
            ca_certs = certifi.where()

        addition_pool_args = {}
        if configuration.assert_hostname is not None:
            addition_pool_args['assert_hostname'] = configuration.assert_hostname  # noqa: E501

        if configuration.retries is not None:
            addition_pool_args['retries'] = configuration.retries

        if maxsize is None:
            if configuration.connection_pool_maxsize is not None:
                maxsize = configuration.connection_pool_maxsize
            else:
                maxsize = 4

        # https pool manager
        if configuration.proxy:
            self.pool_manager = urllib3.ProxyManager(
                num_pools=pools_size,
                maxsize=maxsize,
                cert_reqs=cert_reqs,
                ca_certs=ca_certs,
                cert_file=configuration.cert_file,
                key_file=configuration.key_file,
                proxy_url=configuration.proxy,
                proxy_headers=configuration.proxy_headers,
                **addition_pool_args
            )
        else:
            self.pool_manager = urllib3.PoolManager(
                num_pools=pools_size,
                maxsize=maxsize,
                cert_reqs=cert_reqs,
                ca_certs=ca_certs,
                cert_file=configuration.cert_file,
                key_file=configuration.key_file,
                **addition_pool_args
            ) 
Example 26
Project: python   Author: kubernetes-client   File: rest.py    License: Apache License 2.0 4 votes vote down vote up
def __init__(self, configuration, pools_size=4, maxsize=None):
        # urllib3.PoolManager will pass all kw parameters to connectionpool
        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75  # noqa: E501
        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680  # noqa: E501
        # maxsize is the number of requests to host that are allowed in parallel  # noqa: E501
        # Custom SSL certificates and client certificates: http://urllib3.readthedocs.io/en/latest/advanced-usage.html  # noqa: E501

        # cert_reqs
        if configuration.verify_ssl:
            cert_reqs = ssl.CERT_REQUIRED
        else:
            cert_reqs = ssl.CERT_NONE

        # ca_certs
        if configuration.ssl_ca_cert:
            ca_certs = configuration.ssl_ca_cert
        else:
            # if not set certificate file, use Mozilla's root certificates.
            ca_certs = certifi.where()

        addition_pool_args = {}
        if configuration.assert_hostname is not None:
            addition_pool_args['assert_hostname'] = configuration.assert_hostname  # noqa: E501

        if maxsize is None:
            if configuration.connection_pool_maxsize is not None:
                maxsize = configuration.connection_pool_maxsize
            else:
                maxsize = 4

        # https pool manager
        if configuration.proxy:
            self.pool_manager = urllib3.ProxyManager(
                num_pools=pools_size,
                maxsize=maxsize,
                cert_reqs=cert_reqs,
                ca_certs=ca_certs,
                cert_file=configuration.cert_file,
                key_file=configuration.key_file,
                proxy_url=configuration.proxy,
                **addition_pool_args
            )
        else:
            self.pool_manager = urllib3.PoolManager(
                num_pools=pools_size,
                maxsize=maxsize,
                cert_reqs=cert_reqs,
                ca_certs=ca_certs,
                cert_file=configuration.cert_file,
                key_file=configuration.key_file,
                **addition_pool_args
            ) 
Example 27
Project: data.world-py   Author: datadotworld   File: rest.py    License: Apache License 2.0 4 votes vote down vote up
def __init__(self, pools_size=4, maxsize=4):
        # urllib3.PoolManager will pass all kw parameters to connectionpool
        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75
        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680
        # maxsize is the number of requests to host that are allowed in parallel
        # ca_certs vs cert_file vs key_file
        # http://stackoverflow.com/a/23957365/2985775

        # cert_reqs
        if Configuration().verify_ssl:
            cert_reqs = ssl.CERT_REQUIRED
        else:
            cert_reqs = ssl.CERT_NONE

        # ca_certs
        if Configuration().ssl_ca_cert:
            ca_certs = Configuration().ssl_ca_cert
        else:
            # if not set certificate file, use Mozilla's root certificates.
            ca_certs = certifi.where()

        # cert_file
        cert_file = Configuration().cert_file

        # key file
        key_file = Configuration().key_file

        # proxy
        proxy = Configuration().proxy

        # https pool manager
        if proxy:
            self.pool_manager = urllib3.ProxyManager(
                num_pools=pools_size,
                maxsize=maxsize,
                cert_reqs=cert_reqs,
                ca_certs=ca_certs,
                cert_file=cert_file,
                key_file=key_file,
                proxy_url=proxy
            )
        else:
            self.pool_manager = urllib3.PoolManager(
                num_pools=pools_size,
                maxsize=maxsize,
                cert_reqs=cert_reqs,
                ca_certs=ca_certs,
                cert_file=cert_file,
                key_file=key_file
            ) 
Example 28
Project: whoville   Author: Chaffelson   File: rest.py    License: Apache License 2.0 4 votes vote down vote up
def __init__(self, pools_size=4, maxsize=4):
        # urllib3.PoolManager will pass all kw parameters to connectionpool
        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75
        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680
        # maxsize is the number of requests to host that are allowed in parallel
        # ca_certs vs cert_file vs key_file
        # http://stackoverflow.com/a/23957365/2985775

        # cert_reqs
        if Configuration().verify_ssl:
            cert_reqs = ssl.CERT_REQUIRED
        else:
            cert_reqs = ssl.CERT_NONE

        # ca_certs
        if Configuration().ssl_ca_cert:
            ca_certs = Configuration().ssl_ca_cert
        else:
            # if not set certificate file, use Mozilla's root certificates.
            ca_certs = certifi.where()

        # cert_file
        cert_file = Configuration().cert_file

        # key file
        key_file = Configuration().key_file

        # ssl_context
        ssl_context = Configuration().ssl_context

        # proxy
        proxy = Configuration().proxy

        # https pool manager
        if proxy:
            self.pool_manager = urllib3.ProxyManager(
                num_pools=pools_size,
                maxsize=maxsize,
                cert_reqs=cert_reqs,
                ca_certs=ca_certs,
                cert_file=cert_file,
                key_file=key_file,
                ssl_context=ssl_context,
                proxy_url=proxy
            )
        else:
            self.pool_manager = urllib3.PoolManager(
                num_pools=pools_size,
                maxsize=maxsize,
                cert_reqs=cert_reqs,
                ca_certs=ca_certs,
                cert_file=cert_file,
                key_file=key_file,
                ssl_context=ssl_context
            ) 
Example 29
Project: Cortex-Analyzers   Author: TheHive-Project   File: dnsdb_query.py    License: GNU Affero General Public License v3.0 4 votes vote down vote up
def _query(self, path, before=None, after=None):
        res = []
        url = '%s/lookup/%s' % (self.server, path)

        params = {}
        if self.limit:
            params['limit'] = self.limit
        if before and after:
            params['time_first_after'] = after
            params['time_last_before'] = before
        else:
            if before:
                params['time_first_before'] = before
            if after:
                params['time_last_after'] = after
        if params:
            url += '?{0}'.format(urllib.parse.urlencode(params))

        if self.https_proxy:
            manager = urllib3.ProxyManager(self.https_proxy)
        elif self.http_proxy:
            manager = urllib3.ProxyManager(self.http_proxy)
        else:
            manager = urllib3.PoolManager()

        headers = {
            'Accept': 'application/json',
            'X-Api-Key': self.apikey
        }

        if debug:
            sys.stderr.write(";; query URL =" + url)

        try:
            r = manager.request(method='GET', url=url, headers=headers)
            if r.status == 200:
                json_data = r.data.decode('utf-8')
                if json_data:
                    json_list = json_data.splitlines()
                    for line in json_list:
                        yield json.loads(line)
            else:
                raise QueryError(r.text)

        except (urllib3.exceptions.HTTPError) as e:
            raise QueryError(str(e)) 
Example 30
Project: kaggle-api   Author: Kaggle   File: rest.py    License: Apache License 2.0 4 votes vote down vote up
def __init__(self, configuration, pools_size=4, maxsize=None):
        # urllib3.PoolManager will pass all kw parameters to connectionpool
        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75  # noqa: E501
        # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680  # noqa: E501
        # maxsize is the number of requests to host that are allowed in parallel  # noqa: E501
        # Custom SSL certificates and client certificates: http://urllib3.readthedocs.io/en/latest/advanced-usage.html  # noqa: E501

        # cert_reqs
        if configuration.verify_ssl:
            cert_reqs = ssl.CERT_REQUIRED
        else:
            cert_reqs = ssl.CERT_NONE

        # ca_certs
        if configuration.ssl_ca_cert:
            ca_certs = configuration.ssl_ca_cert
        else:
            # if not set certificate file, use Mozilla's root certificates.
            ca_certs = certifi.where()

        addition_pool_args = {}
        if configuration.assert_hostname is not None:
            addition_pool_args['assert_hostname'] = configuration.assert_hostname  # noqa: E501

        if maxsize is None:
            if configuration.connection_pool_maxsize is not None:
                maxsize = configuration.connection_pool_maxsize
            else:
                maxsize = 4

        # https pool manager
        if configuration.proxy:
            self.pool_manager = urllib3.ProxyManager(
                num_pools=pools_size,
                maxsize=maxsize,
                cert_reqs=cert_reqs,
                ca_certs=ca_certs,
                cert_file=configuration.cert_file,
                key_file=configuration.key_file,
                proxy_url=configuration.proxy,
                **addition_pool_args
            )
        else:
            self.pool_manager = urllib3.PoolManager(
                num_pools=pools_size,
                maxsize=maxsize,
                cert_reqs=cert_reqs,
                ca_certs=ca_certs,
                cert_file=configuration.cert_file,
                key_file=configuration.key_file,
                **addition_pool_args
            )