Python urllib3.HTTPConnectionPool() Examples

The following are 12 code examples of urllib3.HTTPConnectionPool(). 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: http.py    From OpenDoor with GNU General Public License v3.0 7 votes vote down vote up
def __http_pool(self):
        """
        Create HTTP connection pool
        :raise HttpRequestError
        :return: urllib3.HTTPConnectionPool
        """

        try:
            pool = HTTPConnectionPool(self.__cfg.host,
                                      port=self.__cfg.port,
                                      maxsize=self.__cfg.threads,
                                      timeout=Timeout(self.__cfg.timeout, read=self.__cfg.timeout),
                                      block=True)
            if self._HTTP_DBG_LEVEL <= self.__debug.level:
                self.__debug.debug_connection_pool('http_pool_start', pool)
            return pool
        except Exception as error:
            raise HttpRequestError(str(error)) 
Example #2
Source File: test_urllib3.py    From scout_apm_python with MIT License 7 votes vote down vote up
def test_install_fail_no_urlopen_attribute(caplog):
    mock_pool = mock.patch("scout_apm.instruments.urllib3.HTTPConnectionPool")
    with mock_not_attempted, mock_pool as mocked_pool:
        # Remove urlopen attribute
        del mocked_pool.urlopen

        ensure_installed()

    assert len(caplog.record_tuples) == 2
    assert caplog.record_tuples[0] == (
        "scout_apm.instruments.urllib3",
        logging.DEBUG,
        "Instrumenting urllib3.",
    )
    logger, level, message = caplog.record_tuples[1]
    assert logger == "scout_apm.instruments.urllib3"
    assert level == logging.WARNING
    assert message.startswith(
        "Failed to instrument for Urllib3 HTTPConnectionPool.urlopen: AttributeError"
    ) 
Example #3
Source File: requests_wrapper.py    From py-ipfs-http-client with MIT License 6 votes vote down vote up
def __init__(self, *args, **kwargs):
		super().__init__(*args, **kwargs)
		
		# Additionally to adding our variant of the usual HTTP and HTTPS
		# pool classes, also add these for some variants of the default schemes
		# that are limited to some specific address family only
		self.pool_classes_by_scheme = {}
		for scheme, ConnectionPool in (("http", HTTPConnectionPool), ("https", HTTPSConnectionPool)):
			self.pool_classes_by_scheme[scheme] = ConnectionPool
			for name in AF2NAME.values():
				self.pool_classes_by_scheme["{0}+{1}".format(scheme, name)] = ConnectionPool
				self.key_fn_by_scheme["{0}+{1}".format(scheme, name)] = self.key_fn_by_scheme[scheme]

	# These next two are only required to ensure that our custom `scheme` values
	# will be passed down to the `*ConnectionPool`s and finally to the actual
	# `*Connection`s as parameter 
Example #4
Source File: reGeorgSocksProxy.py    From HackTheBox with MIT License 6 votes vote down vote up
def __init__(self, pSocket, connectString):
        Thread.__init__(self)
        self.pSocket = pSocket
        self.connectString = connectString
        o = urlparse(connectString)
        try:
            self.httpPort = o.port
        except:
            if o.scheme == "https":
                self.httpPort = 443
            else:
                self.httpPort = 80
        self.httpScheme = o.scheme
        self.httpHost = o.netloc.split(":")[0]
        self.httpPath = o.path
        self.cookie = None
        if o.scheme == "http":
            self.httpScheme = urllib3.HTTPConnectionPool
        else:
            self.httpScheme = urllib3.HTTPSConnectionPool 
Example #5
Source File: test_urllib3.py    From scout_apm_python with MIT License 6 votes vote down vote up
def test_request_no_absolute_url(caplog, tracked_request):
    ensure_installed()
    delete_absolute_url = delete_attributes(urllib3.HTTPConnectionPool, "_absolute_url")
    with httpretty.enabled(allow_net_connect=False), delete_absolute_url:
        httpretty.register_uri(
            httpretty.GET, "https://example.com/", body="Hello World!"
        )

        http = urllib3_cert_pool_manager()
        response = http.request("GET", "https://example.com")

    assert response.status == 200
    assert response.data == b"Hello World!"
    assert len(tracked_request.complete_spans) == 1
    span = tracked_request.complete_spans[0]
    assert span.operation == "HTTP/GET"
    assert span.tags["url"] == "Unknown" 
Example #6
Source File: httpclient.py    From gnsq with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, host, port, useragent=USERAGENT,
                 connection_class=urllib3.HTTPConnectionPool, **kwargs):
        self.useragent = useragent
        self._connection = connection_class(host, port, **kwargs) 
Example #7
Source File: reGeorgSocksProxy.py    From HackTheBox with MIT License 5 votes vote down vote up
def askGeorg(connectString):
    connectString = connectString
    o = urlparse(connectString)
    try:
        httpPort = o.port
    except:
        if o.scheme == "https":
            httpPort = 443
        else:
            httpPort = 80
    httpScheme = o.scheme
    httpHost = o.netloc.split(":")[0]
    httpPath = o.path
    if o.scheme == "http":
        httpScheme = urllib3.HTTPConnectionPool
    else:
        httpScheme = urllib3.HTTPSConnectionPool

    conn = httpScheme(host=httpHost, port=httpPort)
    response = conn.request("GET", httpPath)
    if response.status == 200:
        if BASICCHECKSTRING == response.data.strip():
            log.info(BASICCHECKSTRING)
            return True
    conn.close()
    return False 
Example #8
Source File: test_gevent.py    From python-sensor with MIT License 5 votes vote down vote up
def setUp(self):
        self.http = urllib3.HTTPConnectionPool('127.0.0.1', port=testenv["wsgi_port"], maxsize=20)
        self.recorder = tracer.recorder
        self.recorder.clear_spans()
        tracer._scope_manager = GeventScopeManager() 
Example #9
Source File: http_urllib3.py    From splunk-elasticsearch with Apache License 2.0 5 votes vote down vote up
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 #10
Source File: http_urllib3.py    From sublime-elasticsearch-client with MIT License 5 votes vote down vote up
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 #11
Source File: test_urllib3.py    From scout_apm_python with MIT License 5 votes vote down vote up
def test_install_fail_no_httpconnectionpool(caplog):
    mock_no_pool = mock.patch(
        "scout_apm.instruments.urllib3.HTTPConnectionPool", new=None
    )
    with mock_not_attempted, mock_no_pool:
        ensure_installed()

    assert caplog.record_tuples == [
        ("scout_apm.instruments.urllib3", logging.DEBUG, "Instrumenting urllib3.",),
        (
            "scout_apm.instruments.urllib3",
            logging.DEBUG,
            "Couldn't import urllib3.HTTPConnectionPool - probably not installed.",
        ),
    ] 
Example #12
Source File: http_urllib3.py    From watchmen with Apache License 2.0 4 votes vote down vote up
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)