Пример #1
0
    def from_url(cls, url, db=None, **kwargs):
        """
        Return a Redis client object configured from the given URL, which must
        use either `the ``redis://`` scheme
        <http://www.iana.org/assignments/uri-schemes/prov/redis>`_ for RESP
        connections or the ``unix://`` scheme for Unix domain sockets.

        For example::

            redis://[:password]@localhost:6379/0
            unix://[:password]@/path/to/socket.sock?db=0

        There are several ways to specify a database number. The parse function
        will return the first specified option:
            1. A ``db`` querystring option, e.g. redis://localhost?db=0
            2. If using the redis:// scheme, the path argument of the url, e.g.
               redis://localhost/0
            3. The ``db`` argument to this function.

        If none of these options are specified, db=0 is used.

        Any additional querystring arguments and keyword arguments will be
        passed along to the ConnectionPool class's initializer. In the case
        of conflicting arguments, querystring arguments always win.
        """
        connection_pool = ConnectionPool.from_url(url, db=db, **kwargs)
        return cls(connection_pool=connection_pool)
Пример #2
0
    def __init__(self,
                 host='localhost',
                 port=6379,
                 db=0,
                 password=None,
                 stream_timeout=None,
                 connect_timeout=None,
                 connection_pool=None,
                 unix_socket_path=None,
                 encoding='utf-8',
                 decode_responses=False,
                 ssl=False,
                 ssl_context=None,
                 ssl_keyfile=None,
                 ssl_certfile=None,
                 ssl_cert_reqs=None,
                 ssl_ca_certs=None,
                 max_connections=None,
                 retry_on_timeout=False,
                 max_idle_time=0,
                 idle_check_interval=1,
                 client_name=None,
                 loop=None,
                 **kwargs):
        if not connection_pool:
            kwargs = {
                'db': db,
                'password': password,
                'encoding': encoding,
                'stream_timeout': stream_timeout,
                'connect_timeout': connect_timeout,
                'max_connections': max_connections,
                'retry_on_timeout': retry_on_timeout,
                'decode_responses': decode_responses,
                'max_idle_time': max_idle_time,
                'idle_check_interval': idle_check_interval,
                'client_name': client_name,
                'loop': loop
            }
            # based on input, setup appropriate connection args
            if unix_socket_path is not None:
                kwargs.update({
                    'path': unix_socket_path,
                    'connection_class': UnixDomainSocketConnection
                })
            else:
                # TCP specific options
                kwargs.update({'host': host, 'port': port})
                if ssl_context is not None:
                    kwargs['ssl_context'] = ssl_context
                elif ssl:
                    ssl_context = RedisSSLContext(ssl_keyfile, ssl_certfile,
                                                  ssl_cert_reqs,
                                                  ssl_ca_certs).get()
                    kwargs['ssl_context'] = ssl_context
            connection_pool = ConnectionPool(**kwargs)
        self.connection_pool = connection_pool
        self._use_lua_lock = None

        self.response_callbacks = self.__class__.RESPONSE_CALLBACKS.copy()
Пример #3
0
 def test_password(self):
     pool = ConnectionPool.from_url('unix://:mypassword@/socket')
     assert pool.connection_class == UnixDomainSocketConnection
     assert pool.connection_kwargs == {
         'path': '/socket',
         'db': 0,
         'password': '******',
     }
Пример #4
0
 def test_db_as_argument(self):
     pool = ConnectionPool.from_url('unix:///socket', db=1)
     assert pool.connection_class == UnixDomainSocketConnection
     assert pool.connection_kwargs == {
         'path': '/socket',
         'db': 1,
         'password': None,
     }
Пример #5
0
 def test_db_in_querystring(self):
     pool = ConnectionPool.from_url('unix:///socket?db=2', db=1)
     assert pool.connection_class == UnixDomainSocketConnection
     assert pool.connection_kwargs == {
         'path': '/socket',
         'db': 2,
         'password': None,
     }
Пример #6
0
 def test_defaults(self):
     pool = ConnectionPool.from_url('unix:///socket')
     assert pool.connection_class == UnixDomainSocketConnection
     assert pool.connection_kwargs == {
         'path': '/socket',
         'db': 0,
         'password': None,
     }
Пример #7
0
 def test_db_in_querystring(self):
     pool = ConnectionPool.from_url('redis://localhost/2?db=3', db='1')
     assert pool.connection_class == Connection
     assert pool.connection_kwargs == {
         'host': 'localhost',
         'port': 6379,
         'db': 3,
         'password': None,
     }
Пример #8
0
 def test_password(self):
     pool = ConnectionPool.from_url('redis://:mypassword@localhost')
     assert pool.connection_class == Connection
     assert pool.connection_kwargs == {
         'host': 'localhost',
         'port': 6379,
         'db': 0,
         'password': '******',
     }
Пример #9
0
 def test_port(self):
     pool = ConnectionPool.from_url('redis://localhost:6380')
     assert pool.connection_class == Connection
     assert pool.connection_kwargs == {
         'host': 'localhost',
         'port': 6380,
         'db': 0,
         'password': None,
     }
Пример #10
0
 def test_hostname(self):
     pool = ConnectionPool.from_url('redis://myhost')
     assert pool.connection_class == Connection
     assert pool.connection_kwargs == {
         'host': 'myhost',
         'port': 6379,
         'db': 0,
         'password': None,
     }
Пример #11
0
 def test_db_as_argument(self):
     pool = ConnectionPool.from_url('redis://localhost', db='1')
     assert pool.connection_class == Connection
     assert pool.connection_kwargs == {
         'host': 'localhost',
         'port': 6379,
         'db': 1,
         'password': None,
     }
Пример #12
0
 def test_extra_querystring_options(self):
     pool = ConnectionPool.from_url('unix:///socket?a=1&b=2')
     assert pool.connection_class == UnixDomainSocketConnection
     assert pool.connection_kwargs == {
         'path': '/socket',
         'db': 0,
         'password': None,
         'a': '1',
         'b': '2'
     }
Пример #13
0
 def test_quoted_hostname(self):
     pool = ConnectionPool.from_url('redis://my %2F host %2B%3D+',
                                    decode_components=True)
     assert pool.connection_class == Connection
     assert pool.connection_kwargs == {
         'host': 'my / host +=+',
         'port': 6379,
         'db': 0,
         'password': None,
     }
Пример #14
0
 def test_quoted_path(self):
     pool = ConnectionPool.from_url(
         'unix://:mypassword@/my%2Fpath%2Fto%2F..%2F+_%2B%3D%24ocket',
         decode_components=True)
     assert pool.connection_class == UnixDomainSocketConnection
     assert pool.connection_kwargs == {
         'path': '/my/path/to/../+_+=$ocket',
         'db': 0,
         'password': '******',
     }
Пример #15
0
 def test_quoted_password(self):
     pool = ConnectionPool.from_url(
         'redis://:%2Fmypass%2F%2B word%3D%24+@localhost',
         decode_components=True)
     assert pool.connection_class == Connection
     assert pool.connection_kwargs == {
         'host': 'localhost',
         'port': 6379,
         'db': 0,
         'password': '******',
     }
Пример #16
0
 def test_extra_querystring_options(self):
     pool = ConnectionPool.from_url('redis://localhost?a=1&b=2')
     assert pool.connection_class == Connection
     assert pool.connection_kwargs == {
         'host': 'localhost',
         'port': 6379,
         'db': 0,
         'password': None,
         'a': '1',
         'b': '2'
     }
Пример #17
0
    def __init__(self,
                 host='localhost',
                 port=6379,
                 db=0,
                 password=None,
                 stream_timeout=None,
                 connect_timeout=None,
                 connection_pool=None,
                 unix_socket_path=None,
                 ssl=False,
                 ssl_keyfile=None,
                 ssl_certfile=None,
                 ssl_cert_reqs=None,
                 ssl_ca_certs=None,
                 max_connections=None,
                 retry_on_timeout=False,
                 *,
                 loop=None):
        if not connection_pool:
            kwargs = {
                'db': db,
                'password': password,
                'stream_timeout': stream_timeout,
                'connect_timeout': connect_timeout,
                'max_connections': max_connections,
                'retry_on_timeout': retry_on_timeout,
                'loop': loop
            }
            # based on input, setup appropriate connection args
            if unix_socket_path is not None:
                kwargs.update({
                    'path': unix_socket_path,
                    'connection_class': UnixDomainSocketConnection
                })
            else:
                # TCP specific options
                kwargs.update({'host': host, 'port': port})

                if ssl:
                    kwargs.update({
                        'ssl_keyfile': ssl_keyfile,
                        'ssl_certfile': ssl_certfile,
                        'ssl_cert_reqs': ssl_cert_reqs,
                        'ssl_ca_certs': ssl_ca_certs,
                    })
            connection_pool = ConnectionPool(**kwargs)
        self.connection_pool = connection_pool
        self._use_lua_lock = None

        self.response_callbacks = self.__class__.RESPONSE_CALLBACKS.copy()