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)
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, }
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, }
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, }
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': '******', }
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, }
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': '******', }
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, }
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, }
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, }
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' }
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': '******', }
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, }
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' }
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': '******', }