예제 #1
0
    def __init__(self,
                 servers,
                 username=None,
                 password=None,
                 timeout=10,
                 session=None,
                 auth_token=None,
                 verify=True,
                 sse_session=None):
        """Create a MarathonClient instance.

        If multiple servers are specified, each will be tried in succession until a non-"Connection Error"-type
        response is received. Servers are expected to have the same username and password.

        :param servers: One or a priority-ordered list of Marathon URLs (e.g., 'http://host:8080' or
        ['http://host1:8080','http://host2:8080'])
        :type servers: str or list[str]
        :param str username: Basic auth username
        :param str password: Basic auth password
        :param requests.session session: requests.session for reusing the connections
        :param int timeout: Timeout (in seconds) for requests to Marathon
        :param str auth_token: Token-based auth token, used with DCOS + Oauth
        :param bool verify: Enable SSL certificate verification
        :param requests.session sse_session: requests.session for event stream connections, which by default enables tcp keepalive
        """
        if session is None:
            self.session = requests.Session()
        else:
            self.session = session
        if sse_session is None:
            self.sse_session = requests.Session()
            keep_alive = socket_options.TCPKeepAliveAdapter()
            self.sse_session.mount('http://', keep_alive)
            self.sse_session.mount('https://', keep_alive)
        else:
            self.sse_session = sse_session
        self.servers = servers if isinstance(servers, list) else [servers]
        self.auth = (username, password) if username and password else None
        self.verify = verify
        self.timeout = timeout

        self.auth_token = auth_token
        if self.auth and self.auth_token:
            raise ValueError(
                "Can't specify both auth token and username/password. Must select "
                "one type of authentication.")
def test_keep_alive_on_newer_requests_no_idle(PoolManager):
    """Show that options are generated correctly from kwargs."""
    socket_opts = [
        (socket.IPPROTO_TCP, socket.TCP_NODELAY, 1),
        (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
        (socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 10),
        (socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 10),
    ]
    with remove_keepidle():
        adapter = socket_options.TCPKeepAliveAdapter(
            idle=30,
            interval=10,
            count=10,
            pool_connections=10,
            pool_maxsize=5,
            pool_block=True,
        )
    PoolManager.assert_called_once_with(num_pools=10,
                                        maxsize=5,
                                        block=True,
                                        socket_options=socket_opts)
    assert adapter.socket_options == socket_opts