Skip to content

Commit af81132

Browse files
pass redis password in the URL
1 parent 8cf035f commit af81132

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

socketio/asyncio_redis_manager.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,14 @@ def _parse_redis_url(url):
1313
p = urlparse(url)
1414
if p.scheme != 'redis':
1515
raise ValueError('Invalid redis url')
16-
if ':' in p.netloc:
17-
host, port = p.netloc.split(':')
18-
port = int(port)
19-
else:
20-
host = p.netloc or 'localhost'
21-
port = 6379
16+
host = p.hostname or 'localhost'
17+
port = p.port or 6379
18+
password = p.password
2219
if p.path:
2320
db = int(p.path[1:])
2421
else:
2522
db = 0
26-
if not host:
27-
raise ValueError('Invalid redis hostname')
28-
return host, port, db
23+
return host, port, password, db
2924

3025

3126
class AsyncRedisManager(AsyncPubSubManager): # pragma: no cover
@@ -53,15 +48,14 @@ class AsyncRedisManager(AsyncPubSubManager): # pragma: no cover
5348
name = 'aioredis'
5449

5550
def __init__(self, url='redis://localhost:6379/0', channel='socketio',
56-
write_only=False, password=None):
51+
write_only=False):
5752
if aioredis is None:
5853
raise RuntimeError('Redis package is not installed '
5954
'(Run "pip install aioredis" in your '
6055
'virtualenv).')
61-
self.host, self.port, self.db = _parse_redis_url(url)
56+
self.host, self.port, self.password, self.db = _parse_redis_url(url)
6257
self.pub = None
6358
self.sub = None
64-
self.password = password
6559
super().__init__(channel=channel, write_only=write_only)
6660

6761
async def _publish(self, data):

tests/test_asyncio_redis_manager.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,37 @@
99
class TestAsyncRedisManager(unittest.TestCase):
1010
def test_default_url(self):
1111
self.assertEqual(asyncio_redis_manager._parse_redis_url('redis://'),
12-
('localhost', 6379, 0))
12+
('localhost', 6379, None, 0))
1313

1414
def test_only_host_url(self):
1515
self.assertEqual(
1616
asyncio_redis_manager._parse_redis_url('redis://redis.host'),
17-
('redis.host', 6379, 0))
17+
('redis.host', 6379, None, 0))
1818

1919
def test_no_db_url(self):
2020
self.assertEqual(
2121
asyncio_redis_manager._parse_redis_url('redis://redis.host:123/1'),
22-
('redis.host', 123, 1))
22+
('redis.host', 123, None, 1))
2323

2424
def test_no_port_url(self):
2525
self.assertEqual(
2626
asyncio_redis_manager._parse_redis_url('redis://redis.host/1'),
27-
('redis.host', 6379, 1))
27+
('redis.host', 6379, None, 1))
28+
29+
def test_password(self):
30+
self.assertEqual(
31+
asyncio_redis_manager._parse_redis_url('redis://:[email protected]/1'),
32+
('redis.host', 6379, 'pw', 1))
2833

2934
def test_no_host_url(self):
30-
self.assertRaises(ValueError, asyncio_redis_manager._parse_redis_url,
31-
'redis://:123/1')
35+
self.assertEqual(
36+
asyncio_redis_manager._parse_redis_url('redis://:123/1'),
37+
('localhost', 123, None, 1))
38+
39+
def test_no_host_password_url(self):
40+
self.assertEqual(
41+
asyncio_redis_manager._parse_redis_url('redis://:pw@:123/1'),
42+
('localhost', 123, 'pw', 1))
3243

3344
def test_bad_port_url(self):
3445
self.assertRaises(ValueError, asyncio_redis_manager._parse_redis_url,

0 commit comments

Comments
 (0)