コード例 #1
0
ファイル: client.py プロジェクト: acidjunk/rpctools
    def __init__(self,
                 uri,
                 key_file=None,
                 cert_file=None,
                 ca_certs=None,
                 validate_cert_hostname=True,
                 extra_headers=None,
                 timeout=None,
                 pool_connections=False,
                 ssl_opts=None):
        """
        :param uri: The endpoint JSON-RPC server URL.
        :param key_file: (Deprecated) Secret key to use for ssl connection.
        :param cert_file: (Deprecated) Cert to send to server for ssl connection.
        :param ca_certs: (Deprecated) File containing concatenated list of certs to validate server cert against.
        :param extra_headers: Any additional headers to include with all requests.
        :param pool_connections: Whether to use a thread-local connection pool for connections.
        :param ssl_opts: Dictionary of options passed to ssl.wrap_socket
        """
        self.logger = logging.getLogger('{0.__module__}.{0.__name__}'.format(
            self.__class__))
        if extra_headers is None:
            extra_headers = {}

        parsed_uri = urlparse(uri)

        self.type = parsed_uri.scheme
        if self.type not in ("http", "https"):
            raise JsonRpcError("unsupported JSON-RPC uri: %s" % uri)

        self.handler = parsed_uri.path
        port = parsed_uri.port or (80 if self.type == 'http' else 443)
        self.host = '{}:{}'.format(parsed_uri.hostname, port)

        if parsed_uri.username and parsed_uri.password:
            auth = '{}:{}'.format(parsed_uri.username, parsed_uri.password)
            auth = base64.encodestring(unquote(auth).encode('ascii'))
            auth = auth.strip()
            extra_headers.update({"Authorization": b"Basic " + auth})

        self.validate_cert_hostname = validate_cert_hostname
        self.ssl_opts = ssl_opts or {}
        deprecated_params = {
            'keyfile': key_file,
            'certfile': cert_file,
            'ca_certs': ca_certs
        }
        for opt, val in deprecated_params.items():
            if val is not None:
                warnings.warn(
                    'key_file, cert_file, and ca_certs arguments are deprecated; use ssl_opts argument instead',
                    DeprecationWarning)
                self.ssl_opts.setdefault(opt, val)

        # TODO: This could probably be a little cleaner :)
        if pool_connections:
            if self.type == "https":
                self.transport = TLSConnectionPoolSafeTransport(
                    timeout=timeout)
            else:
                self.transport = TLSConnectionPoolTransport(timeout=timeout)
        else:
            if self.type == "https":
                self.transport = SafeTransport(
                    timeout=timeout,
                    ssl_opts=self.ssl_opts,
                    validate_cert_hostname=self.validate_cert_hostname)
            else:
                self.transport = Transport(timeout=timeout)

        self.extra_headers = extra_headers
        self.id = 0  # Initialize our request ID (gets incremented for every request)
コード例 #2
0
ファイル: client.py プロジェクト: ftobia/rpctools
    def __init__(self,
                 uri,
                 key_file=None,
                 cert_file=None,
                 ca_certs=None,
                 validate_cert_hostname=True,
                 extra_headers=None,
                 timeout=None,
                 pool_connections=False):
        """
        :param uri: The endpoint JSON-RPC server URL.
        :param key_file: Secret key to use for ssl connection.
        :param cert_file: Cert to send to server for ssl connection.
        :param ca_certs: File containing concatenated list of certs to validate server cert against.
        :param extra_headers: Any additional headers to include with all requests.
        :param pool_connections: Whether to use a thread-local connection pool for connections.
        """
        self.logger = logging.getLogger('{0.__module__}.{0.__name__}'.format(
            self.__class__))
        if extra_headers is None:
            extra_headers = {}

        parsed_uri = urlparse(uri)

        self.type = parsed_uri.scheme
        if self.type not in ("http", "https"):
            raise JsonRpcError("unsupported JSON-RPC uri: %s" % uri)

        self.handler = parsed_uri.path
        self.host = parsed_uri.hostname

        if parsed_uri.username and parsed_uri.password:
            auth = '{}:{}'.format(parsed_uri.username, parsed_uri.password)
            auth = base64.encodestring(unquote(auth).encode('ascii'))
            auth = auth.strip()
            extra_headers.update({"Authorization": b"Basic " + auth})

        self.key_file = key_file
        self.cert_file = cert_file
        self.ca_certs = ca_certs
        self.validate_cert_hostname = validate_cert_hostname

        # TODO: This could probably be a little cleaner :)
        if pool_connections:
            if self.type == "https":
                self.transport = TLSConnectionPoolSafeTransport(
                    timeout=timeout)
            else:
                self.transport = TLSConnectionPoolTransport(timeout=timeout)
        else:
            if self.type == "https":
                self.transport = SafeTransport(
                    key_file=self.key_file,
                    cert_file=self.cert_file,
                    ca_certs=self.ca_certs,
                    validate_cert_hostname=self.validate_cert_hostname)
            else:
                self.transport = Transport(timeout=timeout)

        self.extra_headers = extra_headers
        self.id = 0  # Initialize our request ID (gets incremented for every request)