def requests_session(self):
        if self._requests_session is None:
            # We force pool size to 1 to ensure that only one
            # connection to the data collector which will be
            # maintained due to keep alive and reused.

            config = {}
            config["keep_alive"] = True
            config["pool_connections"] = 1
            config["pool_maxsize"] = 1

            cert_loc = certifi.where()

            self._requests_session = requests.session(config=config, verify=cert_loc)

        return self._requests_session
示例#2
0
    def requests_session(self):
        if self._requests_session is None:
            # We force pool size to 1 to ensure that only one
            # connection to the data collector which will be
            # maintained due to keep alive and reused.

            config = {}
            config['keep_alive'] = True
            config['pool_connections'] = 1
            config['pool_maxsize'] = 1

            cert_loc = certifi.where()

            self._requests_session = requests.session(config=config,
                                                      verify=cert_loc)

        return self._requests_session
        headers["Content-Encoding"] = "deflate"
        level = (len(data) < 2000000) and 1 or 9
        data = zlib.compress(data, level)

    # The 'requests' library can raise a number of exception derived
    # from 'RequestException' before we even manage to get a connection
    # to the data collector. The data collector can the generate a
    # number of different types of HTTP errors for requests.

    try:
        session_config = {}
        session_config["keep_alive"] = True
        session_config["pool_connections"] = 1
        session_config["pool_maxsize"] = 1

        cert_loc = certifi.where()

        session = requests.session(config=session_config, verify=cert_loc)

        r = session.post(url, headers=headers, proxies=proxies, timeout=_collector_timeout, data=data)

        # Read the content now so we can force close the socket
        # connection if this is a transient session as quickly
        # as possible.

        content = r.content

    except requests.RequestException, exc:
        if not _proxy_host or not _proxy_port:
            _logger.warning(
                "Data collector is not contactable. This can be "
示例#4
0
    # If there is no requests session object provided for making
    # requests create one now. We use a transient session to get around
    # designed flaws in the requests/urllib3 modules. See notes for
    # close_requests_session() function above. Note that keep alive
    # must be set to true at this point to ensure that the pool is
    # actually used to allow us to be able to close the connection.

    auto_close_session = False

    if not session:
        session_config = {}
        session_config['keep_alive'] = True
        session_config['pool_connections'] = 1
        session_config['pool_maxsize'] = 1

        cert_loc = certifi.where()

        session = requests.session(config=session_config, verify=cert_loc)

        auto_close_session = True

    # Send the request. We set 'verify' to be false so that when using
    # SSL there is no attempt to do SSL certificate validation. If it
    # were enabled then we would also need the 'certifi' library.
    #
    # The 'requests' library can raise a number of exception derived
    # from 'RequestException' before we even manage to get a connection
    # to the data collector.
    #
    # The data collector can the generate a number of different types of
    # HTTP errors for requests. These are: